Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: What Is Going On Here ?????

by Jonathan (Curate)
on Nov 21, 2000 at 15:42 UTC ( [id://42668]=note: print w/replies, xml ) Need Help??


in reply to What Is Going On Here ?????

When you loop over your array $_ becomes an alias for each item so you are in fact modifying the array.
From perlfaq4

How do I process/modify each element of an array? Use for/foreach: for (@lines) { s/foo/bar/; # change that word y/XZ/ZX/; # swap those letters }


And now, as he looked and saw the whole Hellespont covered with the vessels of his fleet, and all the shore and every plain about Abydos as full as possible of men, Xerxes congratulated himself on his good fortune; but after a little while he wept...
Asked why he was weeping he replied
..."There came upon me," replied he, "a sudden pity, when I thought of the shortness of man's life, and considered that of all this host, so numerous as it is, not one will be alive when a hundred years are gone by."
Recorded by Herodotus Written about 440 B.C.

Replies are listed 'Best First'.
Re: Re: What Is Going On Here ?????
by orthanc (Monk) on Nov 21, 2000 at 15:47 UTC

    I am aware of what you are saying, but my understanding is that the $_ variable of the test_array foreach loop would be in a different scope to the $_ variable in the do_something subroutine.

    Orthanc

      $_ is a global variable. It is best to save it for inner loops, but if you must do it like this give it a seperate value for the scope of do_something with local($_).

      Updated: Okay thanks for correcting me extremly, but it seem to me to make more sense (and is clearer) if $_ is saved for innermost loops.
      For example this could be clearer:
      while (<>) { for (split /-/,$_) { print $_ . "\n"; } }
        Um this isn't exactly true... Run this... $_ isn't GLOBAL, it is LOCALized automatically. The inner and outer loops each have their own $_ but the "current" $_ is still in scope in the supbroutine called...
        #!/usr/bin/perl -w use strict; my @l=qw( a b 3 d ); foreach (@l) { print "$_ = "; foreach (@l) { print "$_ "; } print "\n"; } foreach (@l) { icky(); } sub icky { print "$_ = "; foreach (@l) { print "$_ "; } print "\n"; }

        You don't need to local($_) inside a foreach loop.

        --
        $you = new YOU;
        honk() if $you->love(perl)

      As repson says $_ is a global, to preserve your initial array I guess you'd have to protect it with
      while (local $_ = <IN>) {
      in the do_something sub

      And now, as he looked and saw the whole Hellespont covered with the vessels of his fleet, and all the shore and every plain about Abydos as full as possible of men, Xerxes congratulated himself on his good fortune; but after a little while he wept...
      Asked why he was weeping he replied
      ..."There came upon me," replied he, "a sudden pity, when I thought of the shortness of man's life, and considered that of all this host, so numerous as it is, not one will be alive when a hundred years are gone by."
      The History of Herodotus By Herodotus Written about 440 B.C.
        I think it's a lot more readable to simply say:
        local $_;
        at the top of do_something. No sense in making Perl do localization work for every iteration of the loop.

        Generally (and this is for everyone else), if you use any functions that make use of $_ in this fashion, you should remember to mask its global value while you use it (via local), so that when your function returns, anything that depends on the value of $_ elsewhere won't be disappointed.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-25 05:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found