How to invoke a perl program from a perl program.

Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2005
Posts: 2
Reputation: manju13 is an unknown quantity at this point 
Solved Threads: 0
manju13 manju13 is offline Offline
Newbie Poster

How to invoke a perl program from a perl program.

 
0
  #1
Sep 16th, 2005
I am invoking a perl program from the shell script. the shell script is named as sam.sh

#!/usr/bin/sh
perl check1.pl cr_incr08292005 n n //cr_incr08292005 is the directory under a directory where sam.sh,slp.pl and check1.pl are stored.

The check1.pl is as below. The check1.pl inturn call another perl program slp.pl.

#!/usr/local/bin/perl
$runAutomatic=$ARGV[1];
$skipCaseClean=$ARGV[2];
if( -d $ARGV[0])
{
print("Directory is $ARGV[0]");
}

if ($runAutomatic eq "n"){ print "No run automatic";
}
if ($skipCaseClean eq "n") { print "No skipping bad cases";
$i=1;
}

print "calling";
system "perl slp.pl" || die "Cannot open"

But when i run the main program using the command

sh sam.sh

The output is as below.

This is from slp.pl

Directory is cr_incr08292005

No run automatic

No skipping bad cases

calling


So the problem is when ever the external perl program is invoked "the system command is called first and then rest of the statements in th program is executed". Please provide me the solution for this problem.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,052
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: How to invoke a perl program from a perl program.

 
0
  #2
Sep 18th, 2005
You need to be clear about where your output begins and ends. Your program prints out "This is from slp.pl\n\n"?
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 2
Reputation: manju13 is an unknown quantity at this point 
Solved Threads: 0
manju13 manju13 is offline Offline
Newbie Poster

Re: How to invoke a perl program from a perl program.

 
0
  #3
Sep 19th, 2005
[QUOTE=manju13]I am invoking a perl program from the shell script. the shell script is named as sam.sh

#!/usr/bin/sh
perl check1.pl cr_incr08292005 n n //cr_incr08292005 is the directory under a directory where sam.sh,slp.pl and check1.pl are stored.

The check1.pl is as below. The check1.pl inturn call another perl program slp.pl.

#!/usr/local/bin/perl
$runAutomatic=$ARGV[1];
$skipCaseClean=$ARGV[2];
if( -d $ARGV[0])
{
print("Directory is $ARGV[0]\n\n");
}

if ($runAutomatic eq "n"){ print "No run automatic\n\n";
}
if ($skipCaseClean eq "n") { print "No skipping bad cases\n\n";
$i=1;
}

print "calling\n\n";
system "perl slp.pl" || die "Cannot open"

Ths program slp.pl is as below.
#!/usr/local/bin/perl
print "This is from slp.pl\n\n";

But when i run the main program using the command

sh sam.sh

The output is as below.

This is from slp.pl

Directory is cr_incr08292005

No run automatic

No skipping bad cases

calling


So the problem is when ever the external perl program is invoked "the system command is called first and then rest of the statements in th program is executed". Please provide me the solution for this problem.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 71
Reputation: indienick is an unknown quantity at this point 
Solved Threads: 2
indienick's Avatar
indienick indienick is offline Offline
Junior Poster in Training

Re: How to invoke a perl program from a perl program.

 
0
  #4
Jul 24th, 2006
Ok, try this code fixup:
  1. #!/usr/local/bin/perl -w
  2.  
  3. ############### check1.pl ###############
  4.  
  5. $runAutomatic = $ARGV[1];
  6. $skipCaseClean = $ARGV[2];
  7.  
  8. if ( -d $ARGV[0] ) { print "Directory is $ARGV[0] \n\n"; }
  9. if ( $runAutomatic eq 'n' ) { print "No run automatic. \n\n"; }
  10. if ( $skipCaseClean eq 'n' ) { print "No skipping bad cases. \n\n"; }
  11.  
  12. system("perl slp.pl") or die("Cannot open.");

Here's what I've done:
1. I cleaned up the alignment and formatting of your syntax.
2. I took out the seemingly useless $i = 1; assignment from the third "if" conditional.
3. I literalized the system() command, and threw in some parenthesis for sanity's sake.
4. Most importantly I added a semi-colon after the die() command.

I hope that fixes things up for you.

EDIT: Keep in mind, Perl's syntax is ugly as is, so - and this goes to everyone who writes in Perl - try and make your code as visually appealing and as un-cryptic as possible; one command/small conditional/assignment a line.
Last edited by indienick; Jul 24th, 2006 at 6:04 pm.
Angel-headed hipsters burning for the ancient heavenly connection, to the starry dynamo in the machinery of the night.
-Ginsburg

Don't tell me to "google it" - I already have.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1
Reputation: John Bokma is an unknown quantity at this point 
Solved Threads: 0
John Bokma John Bokma is offline Offline
Newbie Poster

Re: How to invoke a perl program from a perl program.

 
0
  #5
Jul 25th, 2006
An even cleaner one:


  1. #!/usr/local/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. @ARGV == 3 or die "usage: check1.pl DIR AUTOMATIC SKIP";
  7. my ( $dir, $run_automatic, $skip_case_clean ) = @ARGV;
  8.  
  9. -d $dir and print "Directory is $dir\n\n";
  10. $run_automatic eq 'n' and print "No run automatic.\n\n";
  11. $skip_case_clean eq 'n' and print "No skipping bad cases.\n\n";
  12.  
  13. system( 'perl', 'slp.pl' ) == 0
  14. or die "System call failed: $?";
Note that system returns the exit code, which normally is 0, and hence system ... or die as in the previous fixup always dies...
Last edited by John Bokma; Jul 25th, 2006 at 11:21 pm.
I can be hired as a professional Perl programmer and web developer.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC