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

I have a script that I'm working on that I need to get values from the command line. The problem is this - I want to pass along regex values, but they get interpolated on the command line.

For example, I want to pass along the string "/home/rich36/.*" to my perl program. But instead of getting the literal string, I get all the files in that directory.

Now I know that this problem can be solved by putting quotes around the command line arguments, but I was wondering if there was a way to get the same effect within my perl script without having to quote the command line. Any ideas?

Thanks
Rich36

Replies are listed 'Best First'.
Re: Command line issues
by C-Keen (Monk) on Oct 02, 2001 at 21:21 UTC
    I am nor aware of any mechanism that will let you do that, since the shell evaluates the string first before the perl interpreter can even lay hand on your script. Using quotes will prevent the shell from evaluating the asteriks. If there is something else I do not know.

    Hope this helps a little
    C-Keen

Re: Command line issues
by xphase_work (Pilgrim) on Oct 02, 2001 at 21:21 UTC
    The line "/home/rich36/.*" or even /home/rich36/.*(without quotes) is processed(i.e. interpolated) by the shell before it is sent to your perl script. The only way to have that value given to your script is to enclose it in 'single quotes'.

    --xPhase

Re: Command line issues
by clintp (Curate) on Oct 02, 2001 at 21:22 UTC
    Remember, it's the shell that's doing the expansion -- not perl. This happens before your perl script even gets to look at the arguments. The quotes are to prevent the shell from expanding the * and not for perl's benefit at all.
Re: Command line issues
by bluto (Curate) on Oct 02, 2001 at 21:34 UTC
    If you are like me and forget to use both a beginning and ending quote, most shells will also let you escape individual wildcards with something like '\'...
    % find . -name foo\*

    bluto

Re: Command line issues
by Anonymous Monk on Oct 02, 2001 at 21:22 UTC
    Your shell is expanding the wildcards, get a shell which doesn't do it, or check your shell's documentation to see if there is a way to get it to not expand wildcards. Such a shell would be pretty annoying to use, so I wouldn't be surprised if you have trouble finding it. Best solution: just use quotes.
Re: Command line issues
by jeroenes (Priest) on Oct 02, 2001 at 21:22 UTC
    No, it's a shell thing not a perl issue. Only happens if you shut off shell interpolation or if you use the perl shell. here (link updated)
Re: Command line issues
by Rich36 (Chaplain) on Oct 02, 2001 at 21:32 UTC
    Thanks to everyone for their quick response. I guess I'll either have to make sure the users know to quote the regexes or put in a command line interface and have them type the value there.

    Thanks,
    Rich

      Iguess I'll either have to make sure the users know to quote the regexes or put in a command line interface and have them type the value there.

      I would vote for a redesign of your application.
      First, you cannot trust the users to remember quoting and some users maybe anoyed. Second, if you mean by command line interface some
      print "Put in your regular expression here:" $input = <STDIN>;
      you may anoy your user much more, because automatic batch processing will become impossible.
      But maybe I just missunderstood you here.

      Hanamaki
Re: Command line issues
by ariels (Curate) on Oct 03, 2001 at 11:13 UTC
    As noted by others, in UN*X your shell expands wildcards before your program ever gets to see them. This is a Good Thing -- it makes your program look like other UN*X programs to the user.

    If you really want to override this, here's a way. Again, I strongly recommend you don't do this!

    In csh at least, yoou can set the option noglob to turn off wildcard expansion. Use an alias to call your program (and supply this alias to all users). For instance, try this:

    % alias echoit '(set noglob; echo \!*)' % echoit foo* bar* [a-z]? foo* bar* [a-z]?
    Which seems great until the first time somebody tries to run "echoit foo*" from a /bin/sh script (won't work), or expects your program to run in a globbing context because all other programs do this.