in reply to Should we Localize $_ ?

I think it falls on the programmer's shoulders to make sure her code does not expect that the function isn't going to mangle $_. There are just too many places that $_ could get screwed up.

Besides, I like to explicitly define what the item is that I'm dealing with. I find it much more sane to say, for example:

for my $isbn ( @isbns ) { dosomething($isbn);
than just implicitly saying
for ( @isbns ) { dosomething($_);
Besides, in the latter case, you have to ask yourself a question: "Where is the $_ coming from, and what is it?" It should be every programmer's job to eliminate any such potential questions from the minds of future maintainers, even if it's herself.

Can you tell I'm working on my departmental coding standards? :-)

xoxo,
Andy

%_=split/;/,".;;n;u;e;ot;t;her;c; ".   #   Andy Lester
'Perl ;@; a;a;j;m;er;y;t;p;n;d;s;o;'.  #   http://petdance.com
"hack";print map delete$_{$_},split//,q<   andy@petdance.com   >

Replies are listed 'Best First'.
Re (tilly) 2: Should we Localize $_ ?
by tilly (Archbishop) on Jun 14, 2001 at 04:42 UTC
    I respectfully but strongly disagree.

    More specifically I think that you either need to ban most uses of map and grep, or you need to make it a policy that it is the responsibility of the function writer to not pollute global variables needlessly, in particular not to pollute $_. It is the responsibility of the user of the code to test specifically for that (if you have testing suites, it should be properly tested there). But it is the job of the writer of a function to be sure that their functions are good citizens.

    If you are not willing to make this be your policy then I strongly recommend that you ban most natural uses of map and grep from your code base for your sanity. Oh, and I would prefer to work elsewhere, thankyouverymuch.

Re: Re: Should we Localize $_ ?
by frag (Hermit) on Jun 14, 2001 at 01:30 UTC
    This gives you no protection. Witness:
    my @foo = (2, 4, 8); foreach my $num (@foo) { $num *= 2; } $, = ",";print @foo;print "\n";

    Like Ovid says below, this is a 'problem' of aliasing. But it actually isn't a problem unless you assign to $_ (or $num, or whatever your iterator is called), which is typically a deliberate decision, and not usually something that happens by accident. Although I can imagine typos (the olde =/== switcheroo), or some weird precedence error doing this assignment. Also, if your &dosomething operates on its @_ directly, you'll be at risk; i.e., this does the same thing as the above:

    my @foo = (2, 4, 8); foreach my $num (@foo) { &double($num); } $, = ",";print @foo;print "\n"; sub double { $_[0] = 2*$_[0]; return $_[0]; }
    A safe version of &double would be:
    sub double { my $num = $_[0]; $num = 2*$num; return $num; }
    I suspect that this is probably the most likely way the aliasing feature can botch your expected results, but you're all clear so long as all your subs never access their own @_ except to assign it to lexical variables.

    -- Frag.

Re: Re: Should we Localize $_ ?
by mikeB (Friar) on Jun 13, 2001 at 23:26 UTC
    It also prevents self confusion when you later add an inner loop:
    for (@isbns) { for (@$_->{Authors}) { # now $_ is something else :) } }
Re: Re: Should we Localize $_ ?
by Anonymous Monk on Jun 14, 2001 at 18:34 UTC
    Hi petdance, do you have a copy of your dept coding standards by the way? I haven't seen this kind of doc for Perl programs before, and although our Perl team is only three people, it could be *very* interesting... thanks sk8boardboy