in reply to Re^2: Reading multiple files one line at a time from arguments
in thread Reading multiple files one line at a time from arguments

++! I knew I was forgetting something useful.

In that case:

die "Usage: mytest filename [filename [...]]\n" unless @ARGV; while ( <> ) { print "$ARGV($.): $_" } continue { close ARGV if eof }

That will print current file, current line number, and then the line. The continue clause serves to reset the line number before moving on to the next file. Otherwise $. would just keep counting upward, giving a total for all files.

I have no idea why I indented it that way. Just looking for symmetry or something, I guess.


Dave

Replies are listed 'Best First'.
Re^4: Reading multiple files one line at a time from arguments
by choroba (Cardinal) on Jun 19, 2014 at 08:18 UTC
    You can assign to $. directly. It might tell the purpose more clearly.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      It might be clearer, though you would still need to detect eof. And the method I used is the one documented in eof. So I can't decide if I fully agree or not. Either way we're probably relying a little too much on Perl's cleverness, at the expense of legibility. But tinkering with Perl's cleverness can be entertaining at times. :)


      Dave