[pm-h] Interesting Blog Posts on Context & Array vs List

Michael R. Davis mrdvt92 at yahoo.com
Wed Feb 19 12:02:25 PST 2014


> Personally I find context one of the most fascinating aspects of Perl 
> (Maybe someone should do a presentation!) and I must admit I've been 
> guilty of referring to things as being in "Array Context".  Does anyone 
> disagree with these postings or have any additional knowledge to share?

The only case that should not be used is caught by use warnings.  Maybe I just think in "Perl context" nowadays...

perl -Mwarnings -e ' $a =      (5,6,7); print "$a\n"' #scalar assignment of list  => 7 #never use this "feature"...
perl -Mwarnings -e '($a)=      (5,6,7); print "$a\n"' #array  assignment of list  => 5
perl -Mwarnings -e ' $a = @a = (5,6,7); print "$a\n"' #scalar assignment of array => 3
perl -Mwarnings -e '($a)= @a = (5,6,7); print "$a\n"' #array  assignment of array => 5
perl -Mwarnings -e ' $a =    @{[5,6,7]};print "$a\n"' #scalar assignment of defref anon array => 3
perl -Mwarnings -e '($a)=    @{[5,6,7]};print "$a\n"' #array  assignment of defref anon array => 5

I actually don't like the current popular practice of doing

my ($self, $a, $b, $c)=@_;

I much prefer 

my $self = shift;
my $a    = shift;
my $b    = shift;

as I can

my $self = shift;
my $a    = shift || "default";
my $b    = shift // 0;

and actually understand what I did months later.

Mike

mrdvt92 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/mailman/private/houston/attachments/20140219/afe81cf3/attachment.html>


More information about the Houston mailing list