18

I'm using Perl's diamond <> operator to read from files specified on the command line.

I'd like to be able to report messages like "Trouble on line $. of file $FILENAME", but how can I tell which file is currently used by the diamond?

4
  • Just a note: Using $ARGV is dangerous for detecting which file is open, e.g.: mycmd file.1 file.1 file.1 (multiple files with the same name). Stick with using eof.
    – Guidobot
    Sep 19, 2014 at 22:10
  • The perceived problem with “external packages” is frustrating and absurd. Most Perl modules are just source files with a .pm extension, and no installation process is necessary. By default @INC includes the current directory as ., so just copying the requisite files to the same directory as the source will make the package available and fulfill the dependency.
    – Borodin
    Sep 19, 2014 at 23:44
  • @Borodin if something is available straight in the language, I prefer to know about it. As simple as that.
    – PypeBros
    Sep 26, 2014 at 8:57
  • It would be perverse to recommend a solution that required you to download and install a Perl module if the same effect could be achieved without. Your stated preference looks very much like the common incantation that a solution must not involve installing a module, which belies the fact that anything copied from a Stack Overflow solution is also "external". You should also remember that there are many core modules that are installed with Perl by default, and it would be harsh to require a solution that excluded those.
    – Borodin
    Sep 26, 2014 at 9:22

1 Answer 1

19

See perlvar:

    $ARGV

Contains the name of the current file when reading from <> .

But also consider $. in perlvar. If you do this with perl -n it might not turn out the way you want it, because the counter is not reset in the perl -n use case.

$.

Current line number for the last filehandle accessed.

Each filehandle in Perl counts the number of lines that have been read from it. (Depending on the value of $/ , Perl's idea of what constitutes a line may not match yours.) When a line is read from a filehandle (via readline() or <> ), or when tell() or seek() is called on it, $. becomes an alias to the line counter for that filehandle.

You can adjust the counter by assigning to $. , but this will not actually move the seek pointer. Localizing $. will not localize the filehandle's line count. Instead, it will localize perl's notion of which filehandle $. is currently aliased to.

$. is reset when the filehandle is closed, but not when an open filehandle is reopened without an intervening close(). For more details, see I/O Operators in perlop. Because <> never does an explicit close, line numbers increase across ARGV files (but see examples in eof).

You can also use HANDLE->input_line_number(EXPR) to access the line counter for a given filehandle without having to worry about which handle you last accessed.

Mnemonic: many programs use "." to mean the current line number.

Here's an example:

$ perl -nE 'say "$., $ARGV";' foo.pl bar.pl
1, foo.pl
2, foo.pl
3, foo.pl
4, foo.pl
5, foo.pl
6, foo.pl
7, foo.pl
8, foo.pl
9, foo.pl
10, foo.pl
11, foo.pl
12, foo.pl
13, bar.pl
14, bar.pl
15, bar.pl

If you want it to reset, you need to check for eof at the end of your read loop (thanks @Borodin). Also see the perldoc for eof:

$ perl -nE 'say "$., $ARGV"; close ARGV if eof' foo.pl bar.pl
7
  • yep. I knew about $. already. Thanks for $ARGV: for some reason, it evaded my eyes.
    – PypeBros
    Nov 27, 2012 at 13:16
  • @sylvainulg look at the example, it just confused me a lot. It's not reset if the handle is just reopened to another file.
    – simbabque
    Nov 27, 2012 at 13:17
  • ok, so I could want to do $.=1 if $ARGV ne $lastfilename to avoid confusing users ...
    – PypeBros
    Nov 27, 2012 at 13:18
  • 4
    The canonical method is to write close ARGV if eof at the end of your read loop. That will reset $. to zero.
    – Borodin
    Nov 27, 2012 at 13:22
  • 1
    First, I was confused about this But also consider $. in perlvar. part. But the OP indeed included this into the question. I've another use case (checking file names against content patterns) and here the first part of your answer is exactly what I was searching for.
    – Wolf
    Jun 30, 2015 at 10:15

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.