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

Hi Monks, I am trying to write program which will take command line argument and it will be file name, with extention .h or .c or .cpp or .hpp.. What I want to do is if user enters C:/>linecount *.h then I want to open all .h files in that directory! Your help is greatly appreciated Thank you, Prayag
  • Comment on Using * character to open all Files in the directory

Replies are listed 'Best First'.
Re: Using * character to open all Files in the directory
by Zaxo (Archbishop) on Jul 05, 2002 at 22:18 UTC

    Your shell globbing will kick in calling your program from the command line. It should Just Work as you have it. If it doesn't, check your use of the diamond operator, <>

    Within a program, you can use the glob function to get the same effect.

    After Compline,
    Zaxo

Re: Using * character to open all Files in the directory
by vek (Prior) on Jul 05, 2002 at 22:16 UTC
    You haven't mentioned what you've already tried but I assume you know that @ARGV can be your friend. Very simple example:
    #!/usr/bin/perl -w use strict; for (@ARGV) { # do stuff }
    -- vek --

      The c:\> in Prayag's post gives us a good indication that they're doing this in a Windows (or DOS) environment, which (by default) doesn't expand asterisks, etc. (aka "shell globbing").

      Instead, you need to all such expansion yourself, with something like...

      my @files; push @files, glob foreach @ARGV;

      ... which breaks as soon as you want to open a file containing an asterisk or other expansion character.

      Anyhoo, for further discussion, see the recent portable globbing behavior for command-line tool.

          --k.


        FWIW, I haven't found a way of creating a filename with an embedded wildcard char yet.

        This may be possible under *nix, but I wonder what would stop the shell from trying to expand it everytime someone tried to use it? Showing my ignorance of *nix stuff again.

        Kanji++ You're right of course, I must admit that in my haste I did not notice the C:\>.

        Cheers.

        -- vek --