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

Hi, I am trying to do something like this

perl -e '"system(/usr/bin/perl /root/test.pl)"'

The test.pl looks like this

use strict; use warnings; `touch /tmp/1`;

The file /tmp/1 is not geting created. Any clues why and how can I do that?

Replies are listed 'Best First'.
Re: executing perl script using -e
by shmem (Chancellor) on Sep 22, 2014 at 12:53 UTC
    Any clues why and how can I do that?

    Yes. You are giving perl a string and nothing more. Perl looks at the string and happily throws it away.

    # string "system(/usr/bin/perl /root/test.pl)" # program system "/usr/bin/perl /root/test.pl";

    So, omit the double quotes; try instead

    perl -e 'system "/usr/bin/perl /root/test.pl"'

    or this

    perl -e 'system "/usr/bin/perl","/root/test.pl"'

    See system for the difference between the two expressions.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re: executing perl script using -e
by hippo (Archbishop) on Sep 22, 2014 at 12:55 UTC

    Your perl one-liner does not do anything because your quotation marks are in the wrong place. Simply using the -w option would have alerted you to this:

    $ perl -we '"system(/usr/bin/perl /root/test.pl)"' Useless use of a constant (system(/usr/bin/perl /root/test.pl)) in voi +d context at -e line 1.

    If you correct the one-liner you would have more luck. Although, why you call a perl script to invoke a shell to call another perl script is not clear. Perhaps if you can explain what it is you are actually trying to accomplish someone could suggest an alternative approach.

Re: executing perl script using -e
by LanX (Saint) on Sep 22, 2014 at 12:55 UTC
    Why on earth do you want 3 levels of indirection to execute your Perl script?

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

Re: executing perl script using -e
by karlgoethebier (Abbot) on Sep 22, 2014 at 12:56 UTC
    perl -e 'system("/usr/bin/perl /root/test.pl");' host:~ # ls -l /tmp/1 -rw-r--r-- 1 root root 0 Sep 22 14:54 /tmp/1

    BTW: Why do you need to call perl three two times to execute a shell command?

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re: executing perl script using -e
by choroba (Cardinal) on Sep 22, 2014 at 12:56 UTC
    You misplaced the quotes. system expects a string (or a list of strings), so you need to enclose its arguments in quotes. You don't need to quote the whole script twice for the shell, though - once is enough.
    perl -e 'system("/usr/bin/perl /root/test.pl")'
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: executing perl script using -e
by Anonymous Monk on Sep 22, 2014 at 13:10 UTC

    See the Basic debugging checklist, the first item is to use warnings; use strict;. How to apply them to one-liners: hippo already mentioned -w, adding strict and diagnostics you get -wMstrict -Mdiagnostics:

    $ perl -wMstrict -Mdiagnostics -e '"system(/usr/bin/perl /root/test.pl)"' Useless use of a constant ("system(/usr/bin/perl /root/test."...) in void context at -e line 1 (#1) (W void) You did something without a side effect in a context that does nothing with the return value, such as a statement that doesn't return a value from a block, or the left side of a scalar comma operator. Very often this points not to stupidity on your part, but a failure of Perl to parse your program the way you thought it would. ...
Re: executing perl script using -e
by GotToBTru (Prior) on Sep 22, 2014 at 19:53 UTC
    touch /tmp/1

    You invoke perl to run a shell to run perl to run a shell command. Let's just omit the middle man committee.

    1 Peter 4:10