Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 381
Daniel C. Sobral Avatar asked Dec 18 '25 02:12

Daniel C. Sobral


2 Answers

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.

like image 70
Todd Hunter Avatar answered Dec 20 '25 15:12

Todd Hunter


$ARGV contains the name of the file used in <>.

like image 38
Pod Avatar answered Dec 20 '25 17:12

Pod



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!