PBP: 055 Punctuation Variables

The Best Practice is to ‘use English’ for the uncommon punctuation variables.  This has been a hard thing for me to get used to, but I have found it useful.

So much of Perl is riddled with things like @_, $@, $_, $<, $!, etc.  If you know what they mean they’re dense and terse and all those things people tell you are good, but if you don’t know, you’re lost at sea.  Searching for variables that are entirely punctuation is trickier than it could be – most of today’s search engines ignore punctuation.

(Tip: Open the perlvar page, then search on that page.)

The English module means you get to write @ARG, $EVAL_ERROR, $ARG, $REAL_USER_ID, $OS_ERROR instead.  Those are considerably clearer, but they are not perfect.  Particularly $ARG and @ARG, which are entirely different variables with the same name, just in a different name space.  Bad choice, IMO.

The book says “for the less familiar” punctuation variables, which means it’s likely safe to assume the reader knows common ones, like @_, $!, and $_.  They’re very common Perl and used frequently enough they may have been seen before.  $? might even be known if you assume everyone’s a serious shell programmer.  (Unix folks: This is NOT a safe assumption, so knock it off!)

The perlcritic defaults are a little stricter than I like.  Particularly, they complain at $! and $@, which are pretty commonly used variables and pretty well understood.  I’ll tend to add to the exception list instead of turning the rule off entirely.

 

2 Responses to “PBP: 055 Punctuation Variables”

  1. Jay Allen says:

    Pro tip: Use perldoc -v. Example:

    $ perldoc -v ‘$\’ # quote for shell metacharacters
    Handle->output_record_separator( EXPR )
    $OUTPUT_RECORD_SEPARATOR
    $ORS
    $\ The output record separator for the print operator. If defined,
    this value is printed after the last of print’s arguments. Default
    is “undef”.

    Mnemonic: you set $\ instead of adding “\n” at the end of the
    print. Also, it’s just like $/, but it’s what you get “back” from
    Perl.

    Saves sooooo much time…

  2. Jay Allen says:

    Btw, you don’t seem to have Markdown enabled for comments which is a shame. It’s always funny to comment on a post about code with code only to find your code’s whitespace stripped out… :-/ )

Leave a Reply