http://www.perlmonks.org?node_id=813119

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

hello, Id like to know how to run a perl file from within a perl file. From cmd line it is easy: I can go to a folder in C and run ABC.pl with options like "
ABC.pl com1@115200 65
" I am having problem passing these arguments (com1@115200 65) from within another perl file. I am trying to use: "
my @args=(0); my $ filename = "ABC.pl"; @args = ("$filename com1@115200 65"); system(@args) == 0 or die "system @args failed: $?"; "
" The file can be run without the arguments easily. Plz let me know what is going on.

Replies are listed 'Best First'.
Re: running a perl file from within a perl file
by ikegami (Patriarch) on Dec 17, 2009 at 02:50 UTC
    my @cmd = ($filename, 'com1@115200', '65'); system({ $cmd[0] } @cmd) == 0 or die("system @cmd failed: $?\n")
    or
    my @cmd = ($^X, $filename, 'com1@115200', '65'); system({ $cmd[0] } @cmd) == 0 or die("system @cmd failed: $?\n")
Re: running a perl file from within a perl file
by brap (Pilgrim) on Dec 17, 2009 at 02:47 UTC
    # 813119.pl: my @args=(0); my $ filename = "ABC.pl"; @args = ("$filename com1@115200 65"); system(@args) == 0 or die "system @args failed: $?";

    # ABC.pl: print join( ' / ', @ARGV), "\n";
    Mostly works for me. ABC.pl gets called, and displays
    com1 / 65
    I'd recommend quoting the '@' character in your @args assignment, as in:
    @args = ("$filename com1\@115200 65");
    Which changes the output to
    com1@115200 / 65
    What output are you seeing? Which version of perl (perl -V)?
      Hey thanks, for the reply I got it to work.
Re: running a perl file from within a perl file
by matze77 (Friar) on Dec 17, 2009 at 12:46 UTC

    Hello!
    Nitpick:
    Could you please reformat your post a little:
    some code tags "<c> your code here </c>" would be fine ...
    Writeup Formatting Tips
    Its written directly below the writing box
    Thanks & a happy time at he monastery
    MH

      thanks I have modified my post