Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > more inelegancies

Reply
Thread Tools

more inelegancies

 
 
Larry
Guest
Posts: n/a
 
      08-09-2007
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;

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      08-09-2007
On Aug 9, 2:59 pm, Larry <(E-Mail Removed)> wrote:
> 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:eparse 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

 
Reply With Quote
 
 
 
 
Larry
Guest
Posts: n/a
 
      08-09-2007
On Aug 9, 3:35 pm, Paul Lalli <(E-Mail Removed)> wrote:
> 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.... ??

 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      08-10-2007
On Aug 9, 8:57 pm, Larry <(E-Mail Removed)> wrote:
> On Aug 9, 3:35 pm, Paul Lalli <(E-Mail Removed)> wrote:
>
> > 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.... ??


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

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

 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      08-10-2007
On Aug 9, 8:35 pm, Paul Lalli <(E-Mail Removed)> wrote:

> And indeed, B:eparse 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!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Kamaelia 0.4.0 RELEASED - Faster! More Tools! More Examples! More Docs! ;-) Michael Python 4 06-26-2006 08:00 AM
With a Ruby Yell: more, more more! Robert Klemme Ruby 5 09-29-2005 06:37 AM
DVD Verdict reviews: SYLVESTER AND THE MAGIC PEBBLE AND MORE MAGICAL TALES and more! DVD Verdict DVD Video 0 04-07-2005 08:10 AM
Sygate uses more and more memory? Louise Computer Security 0 06-01-2004 05:30 AM
Re: With More Flash More Lumix: using an external flash unit with the FZ1 and other digicams Hans-Georg Michna Digital Photography 4 08-24-2003 06:05 PM



Advertisments