tobold.org

correct • elegant • free

△ comp.lang.perl △

◅ fcntl to lock a file?

perl is a microcosm of computer language theory ▻

Question about using "system" command

In article <5dnl09$8lg@d2.tufts.edu>,
Premkumar Natarajan <pnataraj@emerald.tufts.edu> wrote:
>@args = ("awk","\'{print \$1;}\'",$filename,">",$another_filename);
>system(@args);
>
>I keep getting an error message that says,
>
>awk:syntax error near line 1

When you use

    system $scalar;

then the scalar is interpreted by the shell for quoting, redirection,
and so on.  (If Perl determines that the scalar contains no shell
metacharacters, it is bypassed, but that's just an optimization hack.)

On the other hand, when you use

    system @array;

the shell is not involved at all.  The arguments to the command are
exactly the elements of the array.

So awk is complaining because it is seeing the single quote marks in its
first argument.  You can't simply remove these, because you need the
shell to interpret the redirection later on.  So you should use this.

    system "awk '{print $1}' $filename > $another_filename";

Tim.
--
Tim Goodwin   | "A language needs both intestines and guts." -- Larry Wall

Original headers:

From: tim@pipex.net (Tim Goodwin)
Newsgroups: comp.lang.perl.misc
Subject: Re: Question about using "system" command
Date: 11 Feb 1997 13:31:32 GMT
Organization: UUNET PIPEX
Message-ID: <5dpsbk$1fm@join.news.pipex.net>
References: <5dnl09$8lg@d2.tufts.edu>

△ comp.lang.perl △

◅ fcntl to lock a file?

perl is a microcosm of computer language theory ▻