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

All what's happening is an implicite passing of $_. But passing $_ isn't any different from passing $str. And you do believe that my $str = "..."; chop $str modifies the lexical (I guess you meant lexical variable where you write localized variable) variable, don't you?

Replies are listed 'Best First'.
Re^8: How can I avoid code repetition here
by rovf (Priest) on Oct 09, 2009 at 07:37 UTC
    you do believe that my $str = "..."; chop $str modifies the lexical

    Yes, with *explicit* passing of a lexical, it is easy to understand how it is done; and if the compiler "knows" about chop (as a 'builtin' function), I can understand now how this work for this case. I wonder whether it would also be possible to write a user defined sub which, when called without parameters, acts on $_ even if it was lexically declared. I don't see how this can be done...

    -- 
    Ronald Fischer <ynnor@mm.st>
      sub hello (_) {say "Hello, ", @_ ? $_[0] : $_} hello "world"; my $_ = "mars"; hello; { my $_ = "earth"; hello; } hello; __END__ Hello, world Hello, mars Hello, earth Hello, mars

        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>
      What do you mean?
      $_ = 1; my $_ = 6; sub ford { $_ .= 6; }; warn $_; warn ford(); warn $_; warn $::_; __END__ 6 at - line 4. 66 at - line 5. 66 at - line 6. 1 at - line 7.