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

B. Estrade estrabd at gmail.com
Fri Feb 21 05:13:40 PST 2014


On Wed, Feb 19, 2014 at 2:35 PM, Mark Allen <mrallen1 at yahoo.com> wrote:

> 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!
>


How will this behave if you call the method with foo(@a, at b, at c, at d) versus
foo(\@a,\@b,\@c,\@d) ?  Does list flattening still occur in this case
necessitating the use array references if you want to pass one array per
parameter?

Brett


>
>
>   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/
>
>
>
> _______________________________________________
> 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/20140221/45834a78/attachment.html>


More information about the Houston mailing list