What is good practice for special Perl variables?

First off, does anyone have a complete list of Perl special variables?

Secondly, are there any tasks that are much easier to use? I always turned off $/ for reading in files right away, and $| - to automatically flush buffers, but I'm not sure of any others.

And thirdly, you should use special Perl variables or be more explicit in their coding. Personally, I am a fan of using special variables to control how code behaves, but I have heard others claim that it just confuses things.

+4
source share
6 answers

1) How often do I use:

  • $! is the quintessential for handling I / O errors

  • $@ to handle eval errors when calling improperly designed libraries (such as databases) whose encoders were not careful enough to encode correct error handling other than die

  • $ _ for map / grep blocks, although I 100% agree with the poster above that using it for regular code is not good practice.

  • $ | for wash buffers

2) Regarding the use of punctuation marks and the English language, I will choose Mark Bollinger’s answer above, although the same refutation applies to those who claim that there is no use for using English names.

"if you use Perl, you obviously don’t choose it for readability"

Mark, I believe that this is not always (or rather, almost never) true. Again, 99% of my experience in Perl wrote Perl production code for large companies, 90% of which are full-blown applications instead of 10-line scripts, so my analysis may not apply to other domains. The reasons for thinking like Mark are wrong:
  • Just because I'm not a Perl neophyte (to put it mildly), some noob analyst hired a year ago - or outsourcing a "genius" - probably not. You may not want to confuse them more than they are. “If the code was difficult to write, it would be difficult to read” is not quite high on the list of good relations of professional developers in any language.

  • When I am at 2 am, falling asleep and eliminating the production problem, I really do not want to depend on the ability of my already almost blind eyes to distinguish between $! and $ |. Especially in the code written by the previously mentioned “genius,” who may not have known which one to use and switch.

  • When I read the code left by an unfinished guy who was coughing “reorganized” from a cough from a company a year ago, I would rather focus on the intricacies of logical logic than on readability soup with punctuation.

+5
source

All are documented in perlvar .

Please note that long names can only be used if you use English qw( -no_match_vars ); .

+19
source

Always remember local 'change your variables to punctuation variables. Some of the punctuation variables are useful, others should not be used. For example, $[ should never be used (it changes the base index of arrays, so local $[ = 1; will cause 1 to refer to the first element in a list or array). Others, such as $" , are iffy. You need to balance the utility of not having to manually connect manually. For example, which one is easier to understand?

 local $" = " :: "; #" my $s = "@a / @b / @c\n"; 

vs

 my $sep = " :: "; my $s = join(" / ", join($sep, @a), join($sep, @a), join($sep, @a)) . "\n"; 

or

 my $s = join(" / ", map { join " :: ", @$_ }, \(@a, @b, @c)) . "\n"; 
+7
source

The three that I use the most are $_ , @_ and $! .

I like to use $_ when going through an array, retrieving parameters (as pointed out by Motti, this is actually @_ ), or doing substitutions:

Example 1.1:

 foreach (@items) { print $_; } 

Example 1.2:

 my $prm1 = shift; # implicit use of @_ or @ARGV depending on context 

Example 1.3:

 s/" "/""/ig; # implicit use of $_ 

I am using $! in such cases:

Example 2.1:

 open(FILE, ">>myfile") || die "Error: $!"; 

I agree that this makes the code more confusing for someone new to Perl. But confusing other people is one of the joys of knowing the language! :)

+4
source

The typical ones I use are $_, @_, @ARGV, $!, $/ . I comment very much on others.

Brad notes that $@ also a fairly common variable. (Error value from eval() ).

+2
source

I say that I use them - if you use Perl, you obviously do not select it for reading neophytes. Any more than random developer will most likely have a browser / link window open, and sifting through the perlvar man page in one window is probably no less difficult than searching for definitions of global global or external variables (and assignments !!). As an example, I recently came across new-in-5.10.x capture buffers:

 /^(?<myName>.*)$/; # and later my $capture = %+{'myName'}; 

And figuring out what was happening was no harder than going to parlvar / perlre and reading a little.

I'd rather find a bunch of crazy specials in undocumented code than a bunch of crazy algorithms in undocumented code.

0
source

All Articles