Style

camelCase is bad

Have you ever had to maintain someone else's code?

Have you ever had to maintain code like this?

  1. my $variableThatContainsData =
  2. someSubroutineThatMucksWithData( $someAwfulVariable );

The mixed-case-for-words is called camelCase in the Perl world, and it's generally frowned upon because it makes reading code difficult.

Even with the horrible names, using underscores makes things more readable:

  1. my $variable_that_contains_data =
  2. some_subroutine_that_mucks_with_data( $some_awful_variable );

warnings & strict

For any program you expect to maintain, reuse, or distribute (that is, for any program), you should have the following lines of code:

  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;

Although, if your Perl is older than 5.6, you need to do this instead:

  1. #!/usr/bin/perl -w
  2.  
  3. use strict;

Enabling strict makes Perl complain about uncertain coding constructs, such as undeclared variables, barewords, and "soft" references. These warnings will cause Perl to die.

  1. #!/usr/bin/perl
  2. use strict;
  3.  
  4. $foo = 4; # undeclared variable error
  5. $foo = Bar; # bareword error
  6. my $bat = "foo";
  7. print $$bat; # reference error

Enabling warnings makes Perl complain even more verbosely, but unlike strict, these complaints are not fatal under ordinary circumstances.

  1. #!/usr/bin/perl
  2. use warnings;
  3. $a + 0; # void context warning
  4. # name used once warning
  5. # undef warning
  6. print "program continued\n"; # prints

If you want warnings to be fatal, tell it so:

  1. use warnings FATAL => 'all';
  2. $a + 0; # void warning and then exits
  3. print "program continued\n"; # doesn't print

Want to contribute?

Submit a PR to github.com/petdance/perl101