Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello!
I'm working on a program which examines one or several text files. The files to examine should be stated as an argument at the command line, e.g.:
perl examinetext.pl file*
(and the files names are file1.txt, file2.txt, et c.)

This works when using my Linux system, but not using ActivePerl under XP.
How come? And how do I fix it?
Under XP/AP the 'file*'-argument is interpreted literally as file* and not as the different files, and a "print @ARGV" would mean that it prints just "file*" and not the filenames.

Systems:
Linux Fedora Core 4 (64-bit) Perl 5.8.6
Windows XP (32-bit) Perl 5.8.4

Thanks from a newbie!

Replies are listed 'Best First'.
Re: asterisk problems, and more
by Fletch (Bishop) on Jan 09, 2006 at 15:29 UTC

    Because the Wintendo shell doesn't expand globs; it's up to the receiving program to do so if it wants to understand them. Your options are to get a real shell, or preprocess @ARGV.

    if( $^O eq 'MSWin32' ) { @ARGV = map glob( $_ ), @ARGV; }

      I recommend doing this in your for loop. Instead of:

      for (@ARGV) { }

      Use:

      for ( grep {-f $_} map{ glob($_) } @ARGV ) { }

      This will, in a cross-platform way, return a list of all files in the directory. If you need things that aren't files (like directories), you can drop the grep {-f $_} piece.

      See glob, grep, and map for more.

      <-radiant.matrix->
      A collection of thoughts and links from the minds of geeks
      The Code that can be seen is not the true Code
      "In any sufficiently large group of people, most are idiots" - Kaa's Law
Re: asterisk problems, and more
by murugu (Curate) on Jan 09, 2006 at 15:29 UTC

    Hi,

    use glob function for this.

    use strict; use warnings; my $a = shift; my @a = glob($a); print @a;

    I tested the following code under windows NT using Activestate perl 5.8.7

    Regards,
    Murugesan Kandasamy
    use perl for(;;);

Re: asterisk problems, and more
by svenXY (Deacon) on Jan 09, 2006 at 15:27 UTC
    Hi,
    perl -e "for (<$ARGV[0]>) { print \"$_\n\"}" file*

    Regards,
    svenXY
Re: asterisk problems, and more
by sh1tn (Priest) on Jan 09, 2006 at 16:06 UTC
    You can use the shell interpolate the wanted arguments:
    chomp, push @files, `dir /b $_` for @ARGV; # now *pl file*txt and whatever are interpolated


Re: asterisk problems, and more
by Anonymous Monk on Jan 09, 2006 at 16:53 UTC
    Thank you very much for the help! :-)
Re: asterisk problems, and more
by smokemachine (Hermit) on Jan 09, 2006 at 19:26 UTC