Home > Perl, Programming, Tech > Perl’s predefined variables have long and short equivalents

Perl’s predefined variables have long and short equivalents

Have you ever seen (or written) something like this in your Perl scripts? 

if ( $^E eq "Access is denied" ) #...

I know that I have, and I also know that 2 years later I found that snippet much less readable.

$^E is one of Perl’s predefined variables that reports error information specific to the current OS. However, it has an equivalent/alias that is much more understandable:

$EXTENDED_OS_ERROR

That’s right, $^E  is the same as  $EXTENDED_OS_ERROR

So a better example of the original snippet would be:

if ( $EXTENDED_OS_ERROR eq "Access is denied" ) #...

I’d highly suggest that you always use the long-hand equivalent version of all the predefined Perl variables. Another option is to add the following to the top of your script:

use English qw( -no_match_vars ) ; # Avoids regex performance penalty

http://perldoc.perl.org/English.html

This module provides aliases for the built-in variables whose names no one seems to like to read. Variables with side-effects which get triggered just by accessing them (like $0) will still be affected.

For those variables that have an awk version, both long and short English alternatives are provided. For example, the $/ variable can be referred to either $RS or $INPUT_RECORD_SEPARATOR if you are using the English module.

See perlvar for a complete list of these.

Advertisements
Categories: Perl, Programming, Tech
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s