Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Trouble running Perl script from within a Perl script

Reply
Thread Tools

Trouble running Perl script from within a Perl script

 
 
laredotornado@zipmail.com
Guest
Posts: n/a
 
      07-28-2011
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
 
Reply With Quote
 
 
 
 
J. Gleixner
Guest
Posts: n/a
 
      07-28-2011
On 07/28/11 15:52, http://www.velocityreviews.com/forums/(E-Mail Removed) 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.

 
Reply With Quote
 
 
 
 
RedGrittyBrick
Guest
Posts: n/a
 
      07-28-2011
On 28/07/2011 16:52, (E-Mail Removed) 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
 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      07-28-2011
"(E-Mail Removed)" <(E-Mail Removed)> 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).

 
Reply With Quote
 
ccc31807
Guest
Posts: n/a
 
      07-29-2011
On Jul 28, 11:52*am, "(E-Mail Removed)"
<(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to fetch output of some other script in the current running perl script? ritugoyal12@gmail.com Perl Misc 4 10-27-2007 03:52 PM
shell script with arguments within perl script jay Ruby 7 05-03-2007 08:56 AM
Running Perl Script from within javascript von Javascript 6 02-14-2005 02:40 AM
search within a search within a search - looking for better way...my script times out Abby Lee ASP General 5 08-02-2004 04:01 PM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments