in reply to Re^8: How can I avoid code repetition here
in thread How can I avoid code repetition here

sub hello (_) {say "Hello, ", @_ ? $_[0] : $_} hello "world"; my $_ = "mars"; hello; { my $_ = "earth"; hello; } hello; __END__ Hello, world Hello, mars Hello, earth Hello, mars

Replies are listed 'Best First'.
Re^10: How can I avoid code repetition here
by rovf (Priest) on Oct 12, 2009 at 09:43 UTC

    I now see that you meant the case

    my $_; # lexical declared in outer scope sub f { # $_ used here.... }
    Sorry for my misunderstanding. But in this case, I don't see why my original code would not work for this case?

    -- 
    Ronald Fischer <ynnor@mm.st>
      You're missing the case where $_ is defined in an inner scope.

      Compare:

      sub f {say @_ ? $_[0] : $_} sub g (_) {say @_ ? $_[0] : $_} $_ = "outer"; { my $_ = "inner"; f; g; } __END__ outer inner
      Lacking the _ prototype, you original code would not work for the case of lexical $_ not in the same scope as the function definition.
        Lacking the _ prototype
        Again something new I learned about Perl 5.10! Thanks a lot for telling me...

        -- 
        Ronald Fischer <ynnor@mm.st>