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

for example how can I do this: makeupscript.pl -c *.txt -o outputfile.txt -l asdf.pl how can i take in those *.txt files? how can i do this? examples?
  • Comment on How to do WildChars (*) on the command line for my script

Replies are listed 'Best First'.
Re: How to do WildChars (*) on the command line for my script
by saintmike (Vicar) on May 27, 2004 at 23:40 UTC
    If you're typing
    makeupscript.pl -c *.txt
    on your shell's command line, the shell will expand that to
    makeupscript.pl -c foo.txt bar.txt baz.txt ...
    and pass on the expanded file names to the perl script. However, if you prevent expansion by saying
    makeupscript.pl -c '*.txt'
    then your script will indeed find *.txt somewhere in @ARGS. To do the expansion yourself, check perldoc -f glob.

      If you're using a shell that does expand wildcards for you (eg. cmd.exe), then there are several modules around that will do this for you.

      My favourite is Jenda's G.pm (docs) (ppd & .gz(5.8)).


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      mmmm is it possible to use this with use getopt::long ?
Re: How to do WildChars (*) on the command line for my script
by graff (Chancellor) on May 28, 2004 at 02:58 UTC
    I'm not sure if I get the intent of your example... Did you mean to say you wanted a command line syntax like this:
    makeupscript.pl -c *.txt -o outputfile.txt -l asdf.pl
    In order for that to work, you have two options:

    (1) You could put appropriate quotation marks around the '*.txt' (or "escape" the "*"), so that the shell will treat it as a literal string and pass it directly to the script, rather than trying to expand it to the set of matching file names. (In unix shells, you escape a chosen character by putting backslash in front of it: \*.txt). This way, you have to do a "glob" inside the script to get the set of matching file names.

    (2) You could restructure the syntax, to something like this:

    makeupscript.pl -o outputfile.txt -l asdf.pl *.txt
    Note that this does not use "-c" to identify the "role" of the "*.txt" arg(s) -- "-o" still assigns a role to "outputfile.txt" and "-l" still identifies "asdf.pl" -- and by using either Getopt::Std or Getopt::Long, the relative ordering of these two options doesn't matter (update, to clarify, "-o arg" can preced or follow "-l arg") -- but all other arguments on the command line are assumed to be the list of *.txt files (update, to clarify: both "-o arg" and "-l arg" must precede all such args). The shell expands "*.txt" to the set of matching file names, and they all go into @ARGV (update: and are still in @ARGV after the Getopt work is done).

    (Note that some (most?) shells have a limit on the amount of memory available for argument expansions on a command line. If there are many hundreds or thousands of files that would match, you might not be able to run the command this way.)

Re: How to do WildChars (*) on the command line for my script
by Solo (Deacon) on May 28, 2004 at 02:25 UTC
    There's also the File::Glob and File::DosGlob modules.

    --Solo
    --
    You said you wanted to be around when I made a mistake; well, this could be it, sweetheart.
Re: How to do WildChars (*) on the command line for my script
by inman (Curate) on May 28, 2004 at 08:17 UTC
    Processing command line arguments is handled very well by Getopt::Long. The arguments processed by Getopts::Long are removed from @ARGV. Try changing your command line to expect the files to process as the last set of arguments. e.g.
    myscript.pl -o output.txt -l log.txt [files to process]

    Expanding command line wildcards is either done by the shell (UNIX) or by a module such as Win32::Autoglob for Windows. Failing either of these options, you can also use the glob operator to get an array of filenames.

    This example uses Getopt::Long and Win32::Autoglob

    #! /usr/bin/perl -w use strict; use Win32::Autoglob; use Getopt::Long; my $outputFile = ''; my $logFile = ''; my $result = GetOptions ("output=s" => \$outputFile, "logfile=s" => \$logFile); print "Output=$outputFile\n"; print "Log=$logFile\n"; $,=' '; print "Files to work with:",@ARGV;
      thanks for all your help guys! =)