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

I got stuck with a problem that i created in the process of debugging something else .
I replaced
 sub case_f {       catfile( $TOP_DIR, $_[0], $_[1] ); }
with
 sub case_f {  $_ = catfile( $TOP_DIR, $_[0], $_[1] ); print "The path is $_ "; $_ }
and it broke the code , whereas the following works
 sub case_f {  my $boink = catfile( $TOP_DIR, $_[0], $_[1] ); print "The path is $boink "; $boink }
and it broke the code . Can someone explain ?

Replies are listed 'Best First'.
Re: $_ when does it get set ?
by kennethk (Abbot) on Mar 02, 2009 at 23:55 UTC

    The problem is likely in your calling routine. Since $_ is a global, using it in any non-local context (like a loop variable in a loop that calls a subroutine) can be hazardous. You could fix your code by creating a local copy:

    sub case_f {  local $_ = catfile( $TOP_DIR, $_[0], $_[1] ); print "The path is $_ "; $_ }

    To answer the more general question addressed by your subject line, you can find many details in perlvar. Note that they cover exactly what I said above in the intro to that piece of documentation.