Trouble running Perl script from within a Perl script

Discussion in 'Perl Misc' started by laredotornado@zipmail.com, Jul 28, 2011.

  1. Guest

    Hi,

    I'm using Perl 5.10.1 on Ubuntu Linux 11.04. I want to run a Perl
    script B from within Perl script A. I can run Perl script B fine from
    the bash shell, but when I try and run it within Perl script A, it
    fails to execute with a "No such file or directory " error. I'm
    hoping someone might have some advice about what I'm overlooking.

    Here's how I create and spawn Perl script B ...


    my $cmd = "perl /opt/scripts/selenium/generate_test_suite.pl \"$
    {project} USA Tests - ${module}\" \"$destTestDir\" \"$testSuiteFile\"
    ";
    runShellCommand( $cmd );

    sub runShellCommand {
    my $cmd = shift;
    print "running command $cmd ...\n";
    open(F, "$cmd") or die "Can't execute command \"$cmd\": $!";
    while (<F>) {
    print;
    }
    close(F);
    print "done.\n\n";
    }


    Thanks, - Dave
     
    , Jul 28, 2011
    #1
    1. Advertising

  2. J. Gleixner Guest

    On 07/28/11 15:52, wrote:
    > Hi,
    >
    > I'm using Perl 5.10.1 on Ubuntu Linux 11.04. I want to run a Perl
    > script B from within Perl script A. I can run Perl script B fine from
    > the bash shell, but when I try and run it within Perl script A, it
    > fails to execute with a "No such file or directory " error. I'm
    > hoping someone might have some advice about what I'm overlooking.
    >
    > Here's how I create and spawn Perl script B ...
    >
    >
    > my $cmd = "perl /opt/scripts/selenium/generate_test_suite.pl \"$
    > {project} USA Tests - ${module}\" \"$destTestDir\" \"$testSuiteFile\"
    > ";


    You probably don't need 'perl' there. If you do, then
    generate_test_suite.pl could be fixed, or you *should* use
    the full path to perl.

    You could also use qq, to eliminate escaping the double quotes and
    there's no need for the braces either.

    my $cmd = qq{ /opt/scripts/selenium/generate_test_suite.pl "$project USA
    Tests - $module" "$destTestDir" "$testSuiteFile" };


    > runShellCommand( $cmd );
    >
    > sub runShellCommand {
    > my $cmd = shift;
    > print "running command $cmd ...\n";
    > open(F, "$cmd") or die "Can't execute command \"$cmd\": $!";


    open( f, "$cmd |" ) or die "Can't execute command \"$cmd\": $!";

    perldoc -f open
    perldoc perlopentut


    > while (<F>) {
    > print;
    > }
    > close(F);
    > print "done.\n\n";
    > }


    Another option is backticks:

    my $out = `$cmd`;
    print "$out\ndone\n\n";

    If you want to do something if generate_test_suite.pl dies or exits with
    an error, see perldoc IPC::Open3.
     
    J. Gleixner, Jul 28, 2011
    #2
    1. Advertising

  3. On 28/07/2011 16:52, wrote:
    > Hi,
    >
    > I'm using Perl 5.10.1 on Ubuntu Linux 11.04. I want to run a Perl
    > script B from within Perl script A. I can run Perl script B fine from
    > the bash shell, but when I try and run it within Perl script A, it
    > fails to execute with a "No such file or directory " error. I'm
    > hoping someone might have some advice about what I'm overlooking.
    >
    > Here's how I create and spawn Perl script B ...
    >
    >
    > my $cmd = "perl /opt/scripts/selenium/generate_test_suite.pl \"$
    > {project} USA Tests - ${module}\" \"$destTestDir\" \"$testSuiteFile\"
    > ";
    > runShellCommand( $cmd );
    >
    > sub runShellCommand {
    > my $cmd = shift;
    > print "running command $cmd ...\n";
    > open(F, "$cmd") or die "Can't execute command \"$cmd\": $!";


    `open(F, "$cmd")` opens a file with filename equal to the contents of
    the $cmd string. You want a pipe symbol. See `perldoc -f open` (I think).

    > while (<F>) {
    > print;
    > }
    > close(F);
    > print "done.\n\n";
    > }
    >


    P.S. Your Perl looks rather old. Nowadays most people use lexical
    file-handles and three-argument opens.

    P.P.S If generate_test_suite (or part of it) can be converted into a
    module, this might be a better approach.

    --
    RGB
     
    RedGrittyBrick, Jul 28, 2011
    #3
  4. "" <> writes:
    > Here's how I create and spawn Perl script B ...
    >
    >
    > my $cmd = "perl /opt/scripts/selenium/generate_test_suite.pl \"$
    > {project} USA Tests - ${module}\" \"$destTestDir\" \"$testSuiteFile\"
    > ";
    > runShellCommand( $cmd );
    >
    > sub runShellCommand {
    > my $cmd = shift;
    > print "running command $cmd ...\n";
    > open(F, "$cmd") or die "Can't execute command \"$cmd\": $!";
    > while (<F>) {
    > print;
    > }
    > close(F);
    > print "done.\n\n";
    > }


    Provided that's really all what you want, you can just use

    system($cmd);

    instead of the runShellCommand subroutine. Since the script you are
    starting will use the same standard output channel as its parent
    process, its output will automatically appear where it should and the
    'wait until done' is already done by system (and in perl, this even
    doesn't spawn an intermediate shell except if shell features are
    actually used by the command).
     
    Rainer Weikusat, Jul 28, 2011
    #4
  5. ccc31807 Guest

    On Jul 28, 11:52 am, ""
    <> wrote:
    > I'm using Perl 5.10.1 on Ubuntu Linux 11.04.  I want to run a Perl
    > script B from within Perl script A.


    You might have some requirements that impose some other constraints on
    your app, but if it were me, I would write Perl Script B as a module,
    and 'use' it in Perl Script A. You can either call Perl Script A from
    the command prompt, or create a cron job, but in either case, you can
    call and execute your Perl Script B functions from Perl Script A just
    as you could directly.

    Besides which, if your Perl Script B functions were fundamental in
    some sense, but creating a module you could reuse those functions in
    script after script.

    CC.
     
    ccc31807, Jul 29, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Abby Lee
    Replies:
    5
    Views:
    415
    Abby Lee
    Aug 2, 2004
  2. jay
    Replies:
    7
    Views:
    238
    Chad Perrin
    May 3, 2007
  3. jackster the jackle

    Trouble running sample script with Shoooes

    jackster the jackle, Dec 17, 2009, in forum: Ruby
    Replies:
    0
    Views:
    139
    jackster the jackle
    Dec 17, 2009
  4. Adam Funk
    Replies:
    4
    Views:
    235
    Adam Funk
    Jan 29, 2007
  5. von
    Replies:
    6
    Views:
    130
Loading...

Share This Page