944,035 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Unsolved
  • Views: 12744
  • Perl RSS
Sep 16th, 2005
0

How to invoke a perl program from a perl program.

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
manju13 is offline Offline
2 posts
since Sep 2005
Sep 18th, 2005
0

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

You need to be clear about where your output begins and ends. Your program prints out "This is from slp.pl\n\n"?
Team Colleague
Reputation Points: 1135
Solved Threads: 172
Super Senior Demiposter
Rashakil Fol is offline Offline
2,479 posts
since Jun 2005
Sep 19th, 2005
0

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

[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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
manju13 is offline Offline
2 posts
since Sep 2005
Jul 24th, 2006
0

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

Ok, try this code fixup:
Perl Syntax (Toggle Plain Text)
  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.
Reputation Points: 23
Solved Threads: 2
Junior Poster in Training
indienick is offline Offline
71 posts
since Aug 2005
Jul 25th, 2006
0

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

An even cleaner one:


Perl Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
John Bokma is offline Offline
1 posts
since Jul 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: PERL software engineers NEEDED
Next Thread in Perl Forum Timeline: Perl CSV question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC