[pm-h] Interesting Blog Posts on Context & Array vs List
Mark Allen
mrallen1 at yahoo.com
Wed Feb 19 12:35:53 PST 2014
When subroutine signatures are released in 5.20, you'll be able to do
sub foo ($self, $foo, $bar, $baz) {
$foo ||= 'default';
$baz //= 0;
...;
}
Yay!
On Wednesday, February 19, 2014 2:22 PM, G. Wade Johnson <gwadej at anomaly.org> wrote:
On Wed, 19 Feb 2014 12:02:25 -0800 (PST)
"Michael R. Davis" <mrdvt92 at yahoo.com> wrote:
> > 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
Great example.
To be a bit pedantic, the first is not a "scalar assignment of a list"
it is "comma operator in scalar context". Putting parens around
something only makes it a "list" on the left side of an assignment.
And, that is an odd special case.
For those who can't imagine why you would do such a thing, it often
happens when a sub returns a "list" (which it is in list context) and
the calling code assigns it to a scalar. This completely changes how
the return is interpreted.
> 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.
Although it only matters in a few circumstances, a single 'my'
statement is marginally faster than multiples. I used to do the
separate statement thing, but now I tend to:
my ($self, $foo, $bar, $baz) = @_;
$foo ||= "default";
$baz //= 0;
I vaguely remember something about a cost for the shift, but that may
have been fixed.
The context concept is definitely critical to getting the most out of
Perl.
G. Wade
--
The greatest enemy of knowledge is not ignorance, it is the illusion of
knowledge. -- Stephen Hawking
_______________________________________________
Houston mailing list
Houston at pm.org
http://mail.pm.org/mailman/listinfo/houston
Website: http://houston.pm.org/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/mailman/private/houston/attachments/20140219/9889ad57/attachment-0001.html>
More information about the Houston
mailing list