Operating System - Microsoft
1745807 Members
3690 Online
108722 Solutions
New Discussion юеВ

Perl doesn't glob filename wildcards?

 
SOLVED
Go to solution
Sheldon Smith
HPE Pro

Perl doesn't glob filename wildcards?

Hello, Perl gurus!

I'm running ActivePerl's Perl 5.10.1 on a Windows (Vista) platform. I'm trying to write a simple script to read and process multiple text files. I can pass a enumerated list of file names, but wildcards don't work.

Here's my program reduced to the essentials (read specified files, print last line):

----- myprog.pl -----
while (<>) {
$line = $_ if ($_ ne '');
}
print $line;
-----

> perl myprog.pl test1.txt test2.txt test3.txt
prints the last line of test3.txt.

> perl myprog.pl test*.txt
fails: Can't open test*.txt: Invalid argument at myprog.pl line 1.

Question: Isn't filename globbing tied to the diamond input operator?

Question: Is there a way to have the diamond operator handle the filename glob?

Note: While I am an HPE Employee, all of my comments (whether noted or not), are my own and are not any official representation of the company

Accept or Kudo

2 REPLIES 2
Andrew C Fieldsend
Respected Contributor
Solution

Re: Perl doesn't glob filename wildcards?

It's a Windows thing, I'm afraid. Perl comes from a UNIX background, where filename globbing is done by the shell before passing the resulting list of filenames to the program. Windows doesn't do filename globbing in the shell - each program is expected to handle the arguments as given, and do any wildcard handling itself.

Take a look at the Perl "glob" function for how to do this.
Sheldon Smith
HPE Pro

Re: Perl doesn't glob filename wildcards?

Thanks, Andrew.

After some study of the "glob" function, for the simple case of just filenames and name-globs passed as arguments, the use of 'glob' reduces to the following:

----- myprog.pl -----
@ARGV = <@ARGV>;

# then the rest of the program:
while (<>) {
$line = $_ if ($_ ne '');
}
print $line;
-----

Note: While I am an HPE Employee, all of my comments (whether noted or not), are my own and are not any official representation of the company

Accept or Kudo