Perl Best Practices (excerpt)---02

Source: Internet
Author: User

Chapter Two: Code deployment 00.

the curly braces and parentheses are represented in the K&r style.

That

My @name = (

' Damian ',

' Matthew '

);

For my $name (@names) {

Formy $word (anagrams_of (LC $name)) {

print "$wor \ n";

}

} 00 Ii.

the control keyword and the subsequent opening brackets are separated by blanks

For example:

Formy $result (@results) {

Print_sep ();

Print$result;

}

while ($min < $max) {

My$try = ($max-$min)/2;

if ($value [$try] < $target) {

$max = $try;

}

Else {

$min = $try;

}

} 00 three.

do not separate the program or variable name from the subsequent opening brackets.

That

[email protected] = Get_candidates ($marker);

Candidate:

Formy $i (0..$ #candidates) {

Nextcandiate if Open_region ($i);

$candidates [$i]

= $incumbent {$candidates [$i]{region}};

} 00.

do not use unnecessary parentheses on built-in functions and "reputation" built-in functions.

That

while (My $record = < $results _file>) {

Chomp$record;

My ($name, $votes) = split "\ t", $record;

print ' Votes for ',

substr ($name, 0, 10), # requires parentheses for imposing precedence

": $votes (verified) \ n";

} 00 ng.

separate the complex keys or indexes from the surrounding parentheses.

For example:

$candidates [$i]= $incumbent {$candidates [$i]{get_region ()}}; 00 land.

using whitespace makes the two-dollar operator more visible relative to its operands.

For example:

My $displacement

= $initial _velocity * $time + 0.5 * $acceleration * $time **2; 00 QI.

A semicolon is placed after each statement.

For example:

while (my $line = <>) {

Chomp$line;

if ($line =~ s{\a (\s*) – (. *)}{$1#$2}xms) {

[Email protected], $;

}

Print$line;

} 00 BA.

each value in a multiple-row list is followed by a comma.

For example:

My @dwarves = (

' Happy ',

' Sleepy ',

' Sneezy '

); 00 JIU.

a line of code that uses 78 columns.

Because printing files, legacy VGA display devices, display software, and application optics do not lift these limits, it is not reasonable to format the code as a width greater than 80 columns. 0 0.

use 4 columns to indent the hierarchy. 0 One.

indent with a space, and do not indent with tabs (tab). 0.

never put two statements on the same line. 0 three.

The code should be divided into paragraphs. 0 One restaurant.

don't cling to the else.

The "cling to" else looks like this:

} else {

Correct:

}

else{0 Wu.

the items that correspond to the aligned vertically.

For example:

$name =standardize_name ($name);

$age =time-$birth _date;

$status = ' active '; 0 Yi Lu.

break the verbose expression before the operator.

For example:

Push @steps, $steps [-1]

+ $radial _velocity * $elapsed _time

+ $orbital _velocity * ($phase + $phase _shift)

-$DRAG _coeff * $altitude

; 0 QI.

separate the lengthy expressions in the middle of the statement.

For example:

My $next _step = $steps [-1]

+ $radial _velocity * $elapsed _time

+ $orbital _velocity * ($phase + $phase _shift)

-$DRAG _coeef * $altitue

;

Add_step (\ @steps, $next _step, $elapsed _time); 0 ba.

Be sure to break the lengthy expression at the lowest possible precedence operator.

For example:

Push @steps, $steps [-1]

+ $radial _velocity * $elapsed _time

+ $orbital _velocity

* ($phase + $phase _shift)

-$DRAG _coeff * $altitude

; 0 Yi Jiu.

The lengthy assignment operation is broken before the assignment operator.

For example:

$predicted _val{$current _data_set}[$next _iteration]

= $average + $predicted _change * $fudge _factor; 0 II 0.

arranges cascading ternary operators in columns.

For example:

My $name = defined $customer {name}? $customer {name}

: ' Sir or Madam '

; 0.

The lengthy list is enclosed in parentheses.

For example:

Print (

' Processing ',

Scalar (@items),

' Items at ',

Time

"\ n"

); 0-Ii.

mechanically perform the deployment style of your choice.

Perl has the formatting tools available: Perltidy.

Website: http://perltidy.sourceforge.net

Perl Best Practices (excerpt)---02

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.