develooper Front page | perl.beginners | Postings from August 2009

while (<>) and wildcards on DOS/Windows

Thread Next
From:
Peter Daum
Date:
August 3, 2009 05:44
Subject:
while (<>) and wildcards on DOS/Windows
Message ID:
h56m2j$bbk$1@ger.gmane.org
Hi,

I occasionally have to write Perl scripts that should behave the same on
Unix- and DOS-like Systems. One little problem I encounter there is:

For quick hacks, the "while(<>)" mechanism is very handy, because it
saves a lot of typing. On Unix, I can call a script as a filter, with
filenames or with glob-Patterns without having to worry about the
details - in any case I can just use <> to read the data. On DOS (and
its descendants), this used to be the same - I still have an old binary
of Perl 5.005 lying around, where Perl silently takes over the glob
expansion that on Unix would be done by the shell. Unfortunately, this
obviously is not the case anymore: With more recent Perl versions, when
a script is called with '*.xyz' it will just try to open '*.xyz' and
fail.

The only workaround I know is to do something like:

@ARGV=map { /[\*\?]/ ? glob($_) : $_} @ARGV;
unshift(@ARGV, '-') unless @ARGV;
while (my $fname = shift @ARGV) {
     open(F, $fname)) or die;
     while (defined(my $f=<F>)) {
     ...
     }
}

Is all this really necessary, or is there a better / more elegant way?

Regards,
                             Peter


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About