Search code examples
perl

How do I get the name of the file being read in Perl?


In the following Perl pattern:

while(<>) {
  # do stuff
}

is there a way to get the name of the file that is presently open?

Just to be clear, I expect to receive many args, so that loop will process more than one file. I want the name of the file presently being processed.


Solution

  • It is stored in

    $ARGV
    

    See perldoc perlvar:

    • $ARGV

    It contains the name of the current file when reading from <>.

    However, if are piping in from STDIN, you will get only '-'.

    There is also more discussion on the null filehandle in perldoc perlop.