sleepingsquirrel has asked for the wisdom of the Perl Monks concerning the following question:

I was wondering if there was a some sort of predefined alias which would allow access to $_ from one lexical scope down. If it DWIM, it would be named something like $__ (that's dollar-underscore-underscore). Here's an example. Take...
for my $tmp ("a","b","c") { for (1,2,3) { print "$tmp, $_\n"; } }
...and get rid of the my $tmp...
for ("a","b","c") { for (1,2,3) { print "$__, $_\n"; } }
And on a related note, is there a way to force a closure to bind particular instances of @_ to its creators @_? Example...
sub test { # return a closure to a routine which prints the creator's # first argument and the closure's first argument. my $val=$_[0]; sub { print "$val, $_[0]\n" } }
...replace with...
sub test2 { sub { print "BLANK $_[0]\n" } # ^ # fill in the BLANK with something that refers to # test2's $_[0], not the anonymous sub's }

Replies are listed 'Best First'.
Re: $_, one lexical scope removed
by dave_the_m (Monsignor) on Jul 15, 2004 at 18:13 UTC
    No is the short answer to both of these questions.

    Dave.

Re: $_, one lexical scope removed
by BrowserUk (Patriarch) on Jul 15, 2004 at 23:10 UTC

    I'm not sure where you are going with this, or if it's a good place to go, but it's possible to do something like what you describe in the second part of your question:

    #! perl -slw use strict; sub freaky { my $ref = \@_; return sub { print "$ref->[ 0 ] $_[ 0 ]"; $ref->[ 0 ] = 'joe'; $_[ 0 ] = 'john'; } } my $outerArg = 'bill'; my $freaky = freaky( $outerArg ); my $innerArg = 'fred'; $freaky->( $innerArg ); print "$innerArg $outerArg"; ( $innerArg, $outerArg ) = ( 'kate', 'suzy' ); my $innerArg2 = 'bluto'; $freaky->( $innerArg2 ); print "$innerArg $innerArg2 $outerArg"; __END__ P:\test>374759 bill fred john joe suzy bluto kate john joe

    The difference between this and your example is that the anonymous sub can affect the values of the arguments to the outer sub after the fact.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon
Re: $_, one lexical scope removed
by perrin (Chancellor) on Jul 15, 2004 at 18:42 UTC
    Are you trying to win an obfuscated code contest? Even if these were possible, they wouldn't be a good thing to do.
      Are you trying to win an obfuscated code contest?
      Essentially. I'm thinking of writing a paper entitled "Monads for Perl" and I was trying to make my example code as side-effect free as possible.


      -- All code is 100% tested and functional unless otherwise noted.
        I was trying to make my example code as side-effect free as possible

        Using $_ doesn't mean your code is side-effect free, just that the side-effects are implicit. This is the reason you should use $_ with caution. It can be a nice way to increase clarity, but as your question ably demonstrates, can often introduce much confusion.

Re: $_, one lexical scope removed
by TomDLux (Vicar) on Jul 15, 2004 at 19:16 UTC

    Bonus points for creative thinking, but would they really be useful? dollar underscore underscore reminds me of MicroSoft inventing ... and .... to directly access higher directories. I guess your next step would be $___, $____, $_____ ....

    I find dollar under useful for quickies, but as soon as my program starts growing beyond a dozen lines ( which is fairly early ), I give loop variables names. It helps to know what you think you're dealing with, so that when you discover in the debugger what you actually get, you have some expectation to compare it to.

    If your caller wanted you to know what args it was called with, it would have told you. But if you really find a use for that information---so you can change it when your caller isn't looking?---I suggest putting the information in caller()'s return values.

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

      If your caller wanted you to know what args it was called with, it would have told you.
      Just to be clear, for the last example I wanted the BLANK to be bound to test2's $_[0] and not the caller of the anon sub's $_[0]. (And I realize it probably can't be done.)
      $_[0]="not me"; $f=test2("arg1"); $f->("arg2"); # should print "arg1, arg2"
Re: $_, one lexical scope removed
by jeffa (Bishop) on Jul 16, 2004 at 14:45 UTC

    It seems like a good idea, but if you think about it, which it is it? I always call $_, it -- because that's what it is. It is it. Since there really is only one word, it -- but many it's can be called it -- just exactly which it is it?

    Get it? ;)

    (name your variables)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)