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

At the shell, I can do either of the following to get exactly the same results:
ls -l simple.pl

OR

ls -l "simple.pl"

The only difference being that I decided to quote the argument to ls.

Now, emulating my first shell command with the Perl system cmd is straightforward:

#!/usr/local/bin/perl5 use strict; my $file = 'simple.pl'; # the program being run! my @syscmd = ('ls' , '-l', $file); warn "@syscmd"; system @syscmd;

However, when attempting to emulate the second shell command Perl does something to preserve the quotes and make it a literal filename and thus the file "simple.pl" is not found.

So, the question becomes how does one emulate the second shell command that I issued from the shell using Perl's system command?

Replies are listed 'Best First'.
Re: Quoting arguments to system
by merlyn (Sage) on Oct 21, 2000 at 02:12 UTC
    You don't need to make it quoted. The "argv" of ls is identical on the two shell command invocations. The shell ignores the quotes, because there wasn't any whitespace in the filename word.

    If you want to show two different shell lines, then hand the command to the shell, using a single-arg invocation for system:

    for my $quote (q{}, q{"}) { $cmd = "ls -l ${quote}simple.pl${quote}"; warn $cmd; system $cmd;
    However, I would not recommend doing this in production code. This is just a demo.

    The real question I have is "what are you trying to do, and why do you care about the quotes?".

    -- Randal L. Schwartz, Perl hacker

      Well, one thing I was trying to do was be sure I could emulate anything I did from the shell with Perl's system command. Because the manpage did not shed any light on the distinction you just mentioned, I thought I would ask here.

      Secondarily, and more specific to my problem at hand, we have an age-old Perl script where the guy hand-fashioned the command-line interface. When you want to send a list of args to an option, you do so like this:

      perl5 old-crusty-program.pl -f"arg1 arg2 arg3"
Re: Quoting arguments to system
by clemburg (Curate) on Oct 21, 2000 at 21:37 UTC

    You might also want to read some documentation about what the shell you use is actually doing with your command string before executing the command it contains. There is common ground here, but of course the different shells do different things with your command string, so talking of "the shell" is a bit of a simplification here (and that's just talking about Unix/Linux shells, not to mention all this bizarre other stuff).

    For a quick and wordy start, why not just read through "info bash", or if you like books, try Learning the bash shell, which contains a nice one-page summary of the process that bash goes through in parsing and evaluating your input. Unix Power Tools also has some nice short articles on what happens when the shell "does" your command string.

    Christian Lemburg
    Brainbench MVP for Perl
    http://www.brainbench.com

Re: Quoting arguments to system
by Fastolfe (Vicar) on Oct 24, 2000 at 19:02 UTC
    If you're trying to emulate shell-style string parsing, you may be interested in the shellwords function provided by Text::ParseWords.