Trouble running Perl script from within a Perl script

L

laredotornado

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
 
J

J. Gleixner

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.
 
R

RedGrittyBrick

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.
 
R

Rainer Weikusat

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).
 
C

ccc31807

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top