Home

Awesome

<p align="center"> <h3 align="center">Perl Style Guide</h3> <p align="center">A starting point for Perl development teams to provide consistency through good practices</p> </p>

Summary

This paper is summarized in some good practice guidelines for Perl coding using "post-modern" practices. This work is in progress and any suggestions/contributions are welcome.

This project is just one of several other coding style guides, there is no intention of setting a pattern from it. Please do not take this as absolute truth. The most important thing here is that you and your team feel comfortable with a certain guideline and make use of it.

This paper is a fork of the paper written by Eric Lorenzana, with only a few changes in the sense of organization of the material and some adjustments to current market practices.


Code layout


Good practices

# Don't do this:
my $string =
  "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor aliqua.";

# Do this instead:
my $string = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor aliqua."

my $fn = sub {$_[0] + 1};              # Bad
my $fn = sub { $_[0] + 1 };            # Good
# Good
if (condition) {
  ...
}

else {
  ...
}

# Bad
if (condition) {
  ...
} else {
  ...
}
# Good
sub function {
  if (test) {
    ...
  }
}

# Bad
sub function
{
  if (test)
  {
    ...
  }
}
# Bad
sub my_method {
  my $self = shift;
  my %params = @_;
  ...
}

# Good
sub my_method {
  my ($self, %params) = @_;
  ...
}
my ( $self, $who, @params ) = @_;      # Bad
my ($self, $who, @params) = @_;        # Good

my $self = { name => "Eric", age => 26 };  # Bad
my $self = {name => "Eric", age => 26};    # Good

my @files = qx| ls $str |;             # Bad
my @files = qx|ls $str|;               # Good
my $fn = sub {$_[0] + 1};              # Bad
my $fn = sub { $_[0] + 1 };            # Good

my @items = map {do_something_to $_} @_;    # Bad
my @items = map { do_something_to $_ } @_;  # Good
# Good
my $area = $pi * ($radius ^ 2);

# Bad
my $area = $pi*($radius^2);

# Correct, but bad style
my %hash = (key1, 'val1',
            key2, 'val2',
            key3, 'val3');

# Good
my %hash = (
              key1 => 'val1',
              key2 => 'val2',
              key3 => 'val3'
           );

Contribution


License