1. Perl
  2. coding rules

Perl seminar coding rules

Perl seminars are created according to the following coding rules. Please refer to it.

Style

Indent is 2 in space

sub parse {
  my $str = shift;
  
  my $tree;
  ...

  return $tree;
}

One line can be up to 79 characters

One line is written within 79 characters.

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Space

Do not put spaces inside parentheses ()

my @nums = (2, 4, 5);
my %ages = (kimoto => 20, ken => 25);
my $total = (2 + 3) + 2;
while ($num == 2) {...}
for (my $i = 4; $i < 5; $i++) {...}

Do not put a space inside the bracket []

my $nums = [2, 4, 5];

Do not put spaces inside the hash reference brace {}

my $ages = {kimoto => 20, ken => 25};

When the subroutine is one line, put a space inside the brace

sub ua {LWP::UserAgent->new}

When the sentence is one line, put a space inside the brace

if (condition) {$num = 1}
else {$num = 2}

Do not put a space in front of the comma, put a space behind the comma

my @nums = (2, 4, 5);

Insert spaces to the left and right of the operator

2 + 3
twenty three
twenty three
twenty three
2 ^ 5
2 == 3
2 != 3
2 <3
2> 3
'a'.'b'
'a' eq 'b'
'a' ne 'b'

Insert a space immediately after if statement, unless statement, while statement, and for statement

Insert a space immediately after if statement, unless statement, while statement, for statement.

if (condition) {

}
unless (condition) {

}
for (condition) {

}
while (condition) {

}

If, unless, while, for opening brace {insert a space in front of

if (condition) {

}
unless (condition) {

}
for (condition) {

}
while (condition) {

}

One space is always available

my $num = 1;
my $title = 'Perl';
my $author = 'kimoto';

It looks beautiful if you have equal (=), but it is troublesome considering the trouble of correction, so I always keep one space.

Indent +2 when there are multiple lines

my $str = 'Hello Hello Hello Hello Hello Hello Hello Hello'
  .'Hello Hello Hello Hello Hello Hello Hello Hello Hello'
  . "Hello Hello Hello Hello Hello\n";

It is easy to see if it is aligned at the position of the string, but since it takes time to correct the blank, I try to connect the next line to the +2 position with a blank.

Semicolon

Do not add a semicolon; when the subroutine is on one line

sub ua {LWP::UserAgent->new}

Do not add a semicolon; when the statement is on one line

if (condition) {$num = 1}
else {$num = 2}

String

Use single quotes when the string does not contain variable

my $str = 'Hello World!';

Conditional statement/repeating statement

Use while without using until

while (condition) {

}

Since all conditional statement can be written with while and it is common in other languages, while is used instead of until.

Use while without do ~ while

while (condition) {

}

All statement that can be written with do ~ while can be written with while, and there is a restriction that next and last cannot be used in do, so while is used.

Use for instead of foreach

for (condition) {...}

Since foreach is another name for for, we use for which can be written short.

Modules, Subroutines, Methods

Do not put parentheses for functions with one argument

my $num = int 5;
my $class = ref $obj;

Do not put parentheses on functions and methods that do not require arguments

my $time = time;
my $ua = LWP::UserAgent->new;

Use direct object syntax

my $ua = LWP::UserAgent->new;

It doesn't use indirect object syntax.

exception.

print $fh "Hello";
printf $fh "Hello%d", 3;

Because it is a very general way of writing.

Specify the function to import

use Encode 'encode';

exception. When it is very common not to describe the import.

use Mojolicious::Lite;
use Time::Piece;

If there is only one function to import, enclose it in single quotes

use Encode 'encode';

Use the string list operator qw // when there are multiple functions to import

use Encode qw/encode decode/;

Always use use to load modules

use LWP::UserAgent;
use XML::Simple;

exception. Use require if you want to load the module at runtime.

if ($use_agent) {
  require LWP::UserAgent;
}

However, in most cases I use use because it is fast enough.

Use "use module name ()" if you don't want to import the function

use Carp ();

Comments

#

The format of the comment is to write one , followed by a space, and then the comment

# comment

Comments start at the beginning of a line or just multiple spaces

# comment
  # comment

Write comments before expressions

# Time
my $time = time;

Array/hash

If the elements of the array fit in one line, write in one line

my @animals = ('cat', 'dog', 'mouse');

If the elements of the array do not fit on one line, write each element on one line

my @animals = (
  'cat',
  'dog',
  'mouse',
  ...
);

If the hash element fits on one line, write it on one line

my %ages = (kimoto => 32, ken => 25, asuka => 50);

If the hash elements don't fit on one line, write each element on one line

my %ages = (
  kimoto => 32,
  ken => 25,
  asuka => 50,
  ...
);

Please refer to the coding rules

This is a Perl seminar coding rule. I've written Perl and feel that it's general, easy to understand, beautiful, and short. Please use it as a reference for good coding.

Related Informatrion