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

I'm trying to create a script that will accept the following command line syntax:
microgen -cf *.cfg -cf /users/someone/goofy.cfg -ts *.vh
which will create a list of configfiles (-cf) and a list of textsub files (-ts). The problem is that @ARGV keeps expanding the file globs so I can't tell what switch a given file is supposed to be associated with. I would like to be able to hold off the file-globbing until I've identified which array to stuff the globbed files into. I've come up with an inelegant way of doing this, which is to assume that any files appearing after a given switch belong to that switch's array until I hit a different switch. But that just feels so *wrong*. Any ideas? -Grebki

Replies are listed 'Best First'.
Re: How can I stop ARGV from globbing all over the place?
by broquaint (Abbot) on Jul 23, 2002 at 17:37 UTC
    This is an issue with the command-line, not perl. Two solutions that spring to mind are literal quoting the arguments e.g
    microgen -cf '*.cfg' -cf /users/someone/goofy.cfg -ts '*.vh'
    Or using one of the Getopt:: modules.
    HTH

    _________
    broquaint

Re: How can I stop ARGV from globbing all over the place?
by Abigail-II (Bishop) on Jul 23, 2002 at 17:39 UTC
    Perl doesn't expand command line arguments. Your shell does! Do something like:
    microgen -cf '*.cfg' -cf /users/someone/goofy.cfg -ts '*.vh'

    Abigail

Re: How can I stop ARGV from globbing all over the place?
by Chmrr (Vicar) on Jul 23, 2002 at 17:39 UTC

    Don'y blame perl and @ARGV -- blame your shell. It's the shell which is doing the expansion on the globm before it ever hits perl.

    The method you've described, of assuming it's one mode until told otherwise, isn't wrong at all; in fact, it's rather canonical.

    perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

Re: How can I stop ARGV from globbing all over the place?
by dpuu (Chaplain) on Jul 23, 2002 at 17:45 UTC
    First, you need to be aware that it is your *nix shell that is doing the glob expansion. If you don't want it to do that, then you'll need to use your shell's quoting rules (e.g. -ts "*.vh").

    Second, the perl-thing you might want to look at is the Getopt::Long module. Using this (standard) module, you can process your options as:

    use Getopt::Long; my @cfg_files; my @ts_files; GetOptions("cf=s@" => \@cfg_files, "ts=s@" => \@ts_files);
    Perl will then extract the args into the arrays for you. (but don't use the shell-quotes in this case) --Dave.
Re: How can I stop ARGV from globbing all over the place?
by dimmesdale (Friar) on Jul 23, 2002 at 17:40 UTC
    What I've found *really* helpful for this is the module Getopt::Std, which comes with base perl. However, for your needs, it sounds like you may want to look at Getopt::Long, or even Getopt::Regex.

    The modules allow you to specify what command line arguments are allowed, their types, and does some other pretty fancy stuff if you need it.