more inelegancies

L

Larry

While we are on the subject of inelegancies ... what if I want to
assign the elements of @zam to scalars, but I don't care about
$zam[2]? I'd like to do:

my ($foo, $bar, undef, $baz) = @zam;

but that won't compile, so I'm forced to do:

my ($foo, $bar, $baz);
($foo, $bar, undef, $baz) = @zam;

Any better way?

And furthermore....

What if $foo and $baz are already my'ed... I'd like to do:

($foo, my $bar, $baz) = @wham;

but Perl doesn't like that either, so I have to do:

my $bar;
($foo, $bar, $baz) = @wham;
 
P

Paul Lalli

While we are on the subject of inelegancies ... what if I want to
assign the elements of @zam to scalars, but I don't care about
$zam[2]? I'd like to do:

my ($foo, $bar, undef, $baz) = @zam;

but that won't compile,

Says who?

$ perl -le'
my @zam = qw/alpha beta gamma delta/;
my ($foo, $bar, undef, $baz) = @zam;
print for $foo, $bar, $baz;
'
alpha
beta
delta

Any better way?

Array slices.

$ perl -le'
my @zam = qw/alpha beta gamma delta/;
my ($foo, $bar, $baz) = @zam[0,1,3];
print for $foo, $bar, $baz;
'
alpha
beta
delta

And furthermore....

What if $foo and $baz are already my'ed... I'd like to do:

($foo, my $bar, $baz) = @wham;

but Perl doesn't like that either

Exactly what version of Perl are you running? This, again, works just
fine for me.

$ perl -le'
my @zam = qw/alpha beta gamma delta/;
my ($foo, $baz);
($foo, my $bar, $baz) = @zam[0,1,3];
print for $foo, $bar, $baz;
'
alpha
beta
delta

And indeed, B::Deparse even shows us that it's doing exactly what we
mean (that the second 'my' applies only to $bar, not $baz:

$ perl -MO=Deparse,-p -le'
my @zam = qw/alpha beta gamma delta/;
my ($foo, $baz);
($foo, my $bar, $baz) = @zam[0,1,3];
print for $foo, $bar, $baz;
'
BEGIN { $/ = "\n"; $\ = "\n"; }
(my(@zam) = ('alpha', 'beta', 'gamma', 'delta'));
my($foo, $baz);
(($foo, my($bar), $baz) = @zam[0, 1, 3]);
;
foreach $_ (($foo, $bar, $baz)) {
print($_);
}
-e syntax OK

Paul Lalli
 
L

Larry

Exactly what version of Perl are you running? This, again, works just
fine for me.

??!!! They are working for me too! I don't know what I was
thinking... maybe they used to not work in some older version of Perl
and I never checked.... ??
 
B

Brian McCauley

??!!! They are working for me too! I don't know what I was
thinking... maybe they used to not work in some older version of Perl
and I never checked.... ??

Yes, in older versions (5.5 I think) ISTR I had to say:

(my ($foo, $bar), undef, my($baz)) = @zam;
 
B

Brian McCauley

And indeed, B::Deparse even shows us that it's doing exactly what we
mean
$ perl -MO=Deparse,-p -le' ....
($foo, my $bar, $baz) = @zam[0,1,3];

deparse output...
($foo, my($bar), $baz) = @zam[0, 1, 3];

For this reason whenever I use my() inside a list like this I _always_
put the explicit () on the my even when, as in this case, they are
redundant.

Code needs to be human-readable!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top