Fellow monks:
I have a sub-routine that parses a list of files and returns a data structure. I'd like to write a unix-style filter that takes either a list of files from STDIN or a filename(s) on the command line and feeds the list into the sub. The angle operator (<>) looks like the way to go, but I've got a question regarding it's use.
I'm using the following to do this:
#!/usr/bin/perl use strict; use warnings; while ( <> ) { ... }
Doing the following:
ls *.txt | my_script.pl
sticks each file from the ls into $_, allowing me to create a list of files for the sub, but the following:
my_script.pl text.txt
opens the file(s) and reads it line by line, placing each line into $_, which is not what I want. I like to be able to feed @ARGV right into my sub.
So I wrote the following to create the list of files if the user specifies them on the command line:
#!/usr/bin/perl use strict; use warnings; my @files; if ( @ARGV ) { @files = map { -f $_ ? $_ : () } @ARGV; } else { while ( <> ) { chomp; push @files, $_; } }
Is this a clean way to do this (I know, I know: TMTOWTDI) or is there a better way?
njcodewarrior
In reply to Writing unix-style filters by njcodewarrior
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |