Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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

by Aristotle (Chancellor)
on Oct 24, 2002 at 02:25 UTC ( [id://207608]=note: print w/replies, xml ) Need Help??


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

There is a very significant difference: your second snippet isn't aliasing anything, it's making a local copy.

I tend to use for as a topicalizer for single expressions, as a statement modifier. It would be cumbersome to bloat those into a naked block with a local, and will often not even do what I want since I'm actually intending to change the original variable. A silly example would be s/nit// for $style;

For temporary variables, which was what this thread really was about, I use local. Not only does a for block not do what I want anyway (except (close your eyes) in the perverted for(my $temp) { ... } form (now open them again)) because it aliases a variable rather than give me a fresh, empty one, but it also adds another indentation level, which in my stylistic preference is to be avoided at all cost.

for and local are really orthogonal to each other. Neither can do what the other does, and so debating which one is preferred is moot.

Makeshifts last the longest.

  • Comment on Re^2: Using $_ as a temp var, especially in functions

Replies are listed 'Best First'.
Re: Re^2: Using $_ as a temp var, especially in functions
by BrowserUk (Patriarch) on Oct 24, 2002 at 03:51 UTC

    second snippet isn't aliasing anything

    Good point. It would have to be

    $_= 'value before localising'; my $thingToAlias = 'the quick brown fox'; { local *_= \$thingToAlias; s/brown//; } print $thingToAlias; print $_;

    Giving

    C:\test>test the quick fox value before localising C:\test>

    A silly example would be s/nit// for $style;

    I can't see any advantage in this over $style =~ s/nit//;?

    I appreciate this was just a trivial example, but I can't see where this would be useful as a statement modifier? (No doubt you have a great example up your sleeve:^)

    For temporary variables, which was what this thread really was about, I use local. Not only does a for block not do what I want anyway (except (close your eyes) in the perverted for(my $temp) { ... } form (now open them again)) because it aliases a variable rather than give me a fresh, empty one, but it also adds another indentation level, which in my stylistic preference is to be avoided at all cost.

    for and local are really orthogonal to each other. Neither can do what the other does, and so debating which one is preferred is moot.

    Agreed. They are unrelated, but I was responding to Re: Using $_ as a temp var, especially in functions in the second part of my post rather than the OP.

    I first saw the 1-time for when you showed it to me in a thread about a week ago (which I can't now find?) but I was uncomfortable with the idiom of using a loop for a straight through peice of code. I'm similarly troubled by the use of redo in a bare block or if statement, prefering to see a loop construct where things are going to loop.

    I did spend a while after you first showed me the 1-time for looking for a suitable alternative. And came up with the local *_ = \$variable; (Which in turn was stolen from the Cheap idioms thread.), but promptly forgot the detail as I haven't had a need to use it yet. That's also why I was interested in the arguments against it.

    Who knows, maybe in time I'll get used to these idioms as I have others and go with the flow (sic), but as I said, my devlopment of a personal style, and my investigations of what's possible in perl is ongoing.


    Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!
      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.

        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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-04-18 15:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found