Is there some way to make variables like $a and $b in regard to strict?

Posted by Axeman on Stack Overflow See other posts from Stack Overflow or by Axeman
Published on 2008-09-29T20:16:37Z Indexed on 2010/04/27 22:43 UTC
Read the original article Hit count: 204

Filed under:
|

In light of Michael Carman's comment, I have decided to rewrite the question. Note that 11 comments appear before this edit, and give credence to Michael's observation that I did not write the question in a way that made it clear what I was asking.


Question: What is the standard--or cleanest way--to fake the special status that $a and $b have in regard to strict by simply importing a module?

First of all some setup. The following works:

#!/bin/perl
use strict;
print "\$a=$a\n";
print "\$b=$b\n";

If I add one more line:

print "\$c=$c\n";

I get an error at compile time, which means that none of my dazzling print code gets to run.

If I comment out use strict; it runs fine. Outside of strictures, $a and $b are mainly special in that sort passes the two values to be compared with those names.

my @reverse_order = sort { $b <=> $a } @unsorted;

Thus the main functional difference about $a and $b--even though Perl "knows their names"--is that you'd better know this when you sort, or use some of the functions in List::Util.

It's only when you use strict, that $a and $b become special variables in a whole new way. They are the only variables that strict will pass over without complaining that they are not declared.

: Now, I like strict, but it strikes me that if TIMTOWTDI (There is more than one way to do it) is Rule #1 in Perl, this is not very TIMTOWDI. It says that $a and $b are special and that's it. If you want to use variables you don't have to declare $a and $b are your guys. If you want to have three variables by adding $c, suddenly there's a whole other way to do it.

Nevermind that in manipulating hashes $k and $v might make more sense:

my %starts_upper_1_to_25 
    = skim { $k =~ m/^\p{IsUpper}/ && ( 1 <= $v && $v <= 25 ) } %my_hash
;`

Now, I use and I like strict. But I just want $k and $v to be visible to skim for the most compact syntax. And I'd like it to be visible simply by

use Hash::Helper qw<skim>;

I'm not asking this question to know how to black-magic it. My "answer" below, should let you know that I know enough Perl to be dangerous. I'm asking if there is a way to make strict accept other variables, or what is the cleanest solution. The answer could well be no. If that's the case, it simply does not seem very TIMTOWTDI.

© Stack Overflow or respective owner

Related posts about perl

Related posts about timtowtdi