in reply to Re: Re^2: Using $_ as a temp var, especially in functions
in thread Using $_ as a temp var, especially in functions

You are right, that example is silly, which is why I said so. :^) It's useful (as that thread was discussing btw) for something like $_ eq "pot" and ($_ = "kettle") for $some->{deeply}{nested}{data}{structure}; The straight alternative is
$some->{deeply}{nested}{data}{structure} = "kettle" if $some->{deeply}{nested}{data}{structure} eq "pot";
which irritates my hubris. Localizing here is better than that, but it's just too much red tape for my taste:
{ local *_ = \$some->{deeply}{nested}{data}{structure}; $_ = "kettle" if $_ eq "pot"; }

local *_ = \$something; is not a language construct, I have to brainparse it and understand what's going on to read the code.

I see where you object to a loop that has only one iteration, although I find the use of redo in a naked block far worse than a single-iteration for loop: the redo conceils the loop character because you don't know that that naked block is one until you read to an arbitrary point inside it. for used as a topicalizer on the other hand makes it immediately obvious that we're only looking at a single iteration.

The example you gave where a function's return value is topicalized misses the point. You topicalize a variable when you intend to modify it - doing that to a function return value you haven't saved is pointless. You might of course do something like this:

my $stuff; for(return_value()) { chomp; s/foo//g; tr/x/y/; $stuff = $_; }
But if I had to maintain that I'd hunt you down and kill you. That obviously should have been
my $stuff = return_value(); for($stuff) { chomp; s/foo//g; tr/x/y/; }

For the record, I actually don't use the long construct with the for block frequently either. It does come in very handy when it's appropriate though. (I got the idea of using for as a topicalizer from the man himself actually - he mentioned this in an interview, where he said it Perl has a topicalizer, for, which lets people say "it", ie $_, without specifying the subject over and over.)

That's what I have to say on the issue. YMMV :-)

Makeshifts last the longest.

Replies are listed 'Best First'.
(Re:)+ Using $_ as a temp var, especially in functions
by rir (Vicar) on Oct 24, 2002 at 13:23 UTC
    Could you please expand on your thoughts on:
    my $stuff; for(return_value()) { chomp; s/foo//g; tr/x/y/; $stuff = $_; }
    versus
    my $stuff = return_value(); for($stuff) { chomp; s/foo//g; tr/x/y/; }
    I agree that the first form is lousy, but since the second snippet is not equivalent I am not sure what you dislike about the first version.
      I'm not sure what difference you are referring to. $stuff will contain the the same value after both snippets, so at least with respect to intent they are equivalent. What I can see is that the first one will have a hidden side effect when return_value() produces an Lvalue - which for me is just another reason to shun that version. Is there something I missed?

      Makeshifts last the longest.

        The context around &return_value differs.

        Other than its memory space and the time taken to create it, what concerns you about the lvalue? Unless there is more I wouldn't call this a side-effect, as many things are implemented with intermediate lvalues. Am I missing something?

        Having not talked much about 'lvalues' it is possible that my usage is baroque. I'd say altering a named value inadvertently is a side-effect. Your usage just seems extremely strict: like saying creating $main::val has the side-effect of creating a whole typeglob. Is this type of usage common? I see its correctness and how compiler writers would use it as I understand your usage here.

        I agree with your preference, but I have never used or seen the do2dollar_ for ($var); constructions you have been talking about. I dislike assigning $stuff as the last operation. It just doesn't read well, the invisible $_ seems much better used in the second version. So again I ask, what are the reasons for your preference?

        I am going to read this thread to find out more about this usage of for. My first reaction is suspection but I also like a less indented style of coding.

        #!/usr/bin/perl -w use strict; sub return_value { return "" unless wantarray; return ("this is a value", "xxx", "yyy", "zzz") ; }; my $stuff; for ( return_value() ) { chomp; s/foo//g; tr/x/y/; $stuff = $_; } print "\$stuff >$stuff<\n"; $stuff = return_value(); for ($stuff) { chomp; s/foo//g; tr/x/y/; } print "\$stuff >$stuff<\n"; # the output __DATA__ $stuff >zzz< $stuff ><