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

If you didn't care about what file name you're processing
Even if you did, you could retrieve it from $ARGV, see perlvar.
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^3: Reading multiple files one line at a time from arguments
by davido (Cardinal) on Jun 19, 2014 at 08:14 UTC

    ++! 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

      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