Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
Operating System - Microsoft
cancel
Showing results for 
Search instead for 
Did you mean: 

Perl doesn't glob filename wildcards?

SOLVED
Go to solution
Sheldon Smith
Honored Contributor

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 work for Hewlett Packard Enterprise, all of my comments (whether noted or not), are my own and are not any official representation of the company.
----------
If my post was useful, click on my KUDOS! thumb below!
2 REPLIES
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
Honored Contributor

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 work for Hewlett Packard Enterprise, all of my comments (whether noted or not), are my own and are not any official representation of the company.
----------
If my post was useful, click on my KUDOS! thumb below!