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

Is there any way to show the implicit variables in a code? Something that would take the following code:
my $filename = shift; if ($line = /something/) { } foreach (@arr) { if (/abc/) { print; } } sub foo { my $x = shift; }
And turn it to something like this:
my $filename = shift @ARGV; if ($line = $_ =~ /something/) { } foreach $_ (@arr) { if ($_ =~ /abc/) { print $_; } } sub foo { my $x = shift @_; }
I looked at B::Deparse but I could not convince it.

Replies are listed 'Best First'.
Re: Showing implicit variables?
by toolic (Bishop) on Feb 27, 2009 at 18:39 UTC
    B::Deparse does a pretty good job for me:
    $ cat ./746989.pl my $filename = shift; if ($line = /something/) { } foreach (@arr) { if (/abc/) { print; } } sub foo { my $x = shift; } $ $ perl -MO=Deparse ./746989.pl my $filename = shift @ARGV; if ($line = /something/) { (); } foreach $_ (@arr) { if (/abc/) { print $_; } } sub foo { my $x = shift @_; } ./746989.pl syntax OK $ $ perl -v This is perl, v5.8.5 built for x86_64-linux-thread-multi

    Did you really mean $line =~ /something/?

      Oh this is funny. The original question I wanted to post was only about this code:
      if ($line = /something/) { }
      that I checked with B::Deparse and which did not show the implicit use of $_. The other examples (the implicit use of @_ and @ARGV) I added only to the post and did not test it. I should have done it before posting.

      So yes, those work for me as well but the one I originally wanted does not work.

      My intention was to ask about the various places where implicit variables are used. As I can see B::Deparse shows @_ and @ARGV but does not show the use of $_.

      So let's see if there is a tool that can show the $_ where it is used implicitly?