Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: Comparing two arrays

by GrandFather (Saint)
on Feb 21, 2015 at 03:55 UTC ( [id://1117395]=note: print w/replies, xml ) Need Help??


in reply to Re: Comparing two arrays
in thread Comparing two arrays

my $var; for $var (...) { }

is really bad because the two instances of $var are not the same animal. The recommended code is:

for my $var (...) { }

The problem is that the first version of the code makes it look like the $var declared before the for loop is the one used in the loop and might even retain the last value it was assigned in the loop. It's not and it doesn't.

The loop variable ($var in the sample code) is an alias for each value looped over. In some sense it's not a real variable at all! To see the alias in action try:

use strict; use warnings; my @array = (1 .. 10); for my $var (@array) { $var *= 10; } print "@array";

Did you notice that the array values were altered! $var becomes each array element in turn. What you do to $var you do to the current array element.

For the vast majority of Perl code you aren't aware of this aliasing trick. Sometimes it is really useful and sometimes, if you don't know about it, it is really nasty. The same thing happens in map and grep where the default variable $_ is aliased in turn to each list element processed.

Aside from all that, don't declare multiple variables on a single line. As a general thing it's a clue that you are declaring things in the wrong place, and if it's the right place the declaration is important enough that it should be one variable per line. Along with choosing good variable names, managing variable scope (and therefore where variables are declared) are important parts of good programming style.

Perl is the programming world's equivalent of English

Replies are listed 'Best First'.
Re^3: Comparing two arrays
by Athanasius (Archbishop) on Feb 21, 2015 at 04:35 UTC

    Hello GrandFather,

    This is an excellent point! In:

    my $var; for $var (...) {...}

    it’s as though there were an implicit for local $var (...) {...} — except (a) that syntax is illegal, and (b) local doesn’t create an alias (which in C++ would be called a reference) as the for loop does. But thinking of what’s going on as an implicit local does, perhaps, explain why the strict pragma is fooled into thinking $var has been declared:

    14:27 >perl -Mstrict -wE "my $var; for $var (1 .. 3) { say $var; } say + $var;" 1 2 3 Use of uninitialized value $var in say at -e line 1. 14:28 >

    Is there any way to get strict, warnings, or something similar to flag this as a probable error?

    BTW, congratulations on your promotion to Sage!

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1117395]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-19 23:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found