Home > Archive > PERL Beginners > April 2004 > Running another program in current program
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Running another program in current program
|
|
| Durai 2004-04-29, 10:09 am |
| Hello All,
In shell script file, I can call another program & checked the exit status.
Like:
../prog2
if [ $? -ne 0 ]
then
echo "Error"
exit
fi
How to do in perl?
ANy help much appreciated.
Regs,
Durai.
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.665 / Virus Database: 428 - Release Date: 4/21/2004
| |
| Durai 2004-04-29, 10:09 am |
| Hi,
Thanks for the reply. Sorry for asking this one. But how to run
another perl prog?
Should I have to use system() function? Or is there any other way?
Thanks,
Durai.
----- Original Message -----
From: "Jose Alves de Castro" <jcastro@telbit.pt>
To: "Durai" <tech_durai@yahoo.com>
Cc: <beginners@perl.org>
Sent: Thursday, April 29, 2004 3:30 PM
Subject: Re: Running another program in current program
> It's the same variable :-)
>
> $? The status returned by the last pipe close, backtick (``) com-
> mand, successful call to wait() or waitpid(), or from the sys-
> tem() operator.
>
> For more info, man perlvar
>
> If you care to give it a test, here it goes:
>
> perl -e '`ls`;print $?'
> # prints "0" (hopefully)
>
> perl -e '`nonexistingcommand`;print $?'
> # prints "1" (unless you have some strange command on your machine...)
>
> HTH,
>
> jac
>
> On Thu, 2004-04-29 at 11:08, Durai wrote:
status.[color=darkred]
> --
> José Alves de Castro <jcastro@telbit.pt>
> Telbit - Tecnologias de Informação
>
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.665 / Virus Database: 428 - Release Date: 4/21/2004
| |
| Durai 2004-04-29, 10:09 am |
| Hi,
I have written many test cases using shell script. But now I am
writting in perl. I have done like the following one:
open(FILE_LOG,">log_file");
print FILE_LOG `perl $PRODUCT_DIR/tests/$MAIN_PRODUCTS_TESTS_STR/run`
if( $? == 0)
{
print "Error";
exit 0;
}
Is it correct?
In shell script, I did like the following one:
. $PRODUCT_DIR/tests/$MAIN_PRODUCTS_TESTS_STR/run 1>file_log 2>file_log
if [ `echo $?` = 0 ]
then
echo "Error";
exit
fi
What is the equivalent code in perl for above shell script?
Thanks,
Durai.
----- Original Message -----
From: "Jose Alves de Castro" <jcastro@telbit.pt>
To: "Durai" <tech_durai@yahoo.com>
Cc: <beginners@perl.org>
Sent: Thursday, April 29, 2004 3:30 PM
Subject: Re: Running another program in current program
> It's the same variable :-)
>
> $? The status returned by the last pipe close, backtick (``) com-
> mand, successful call to wait() or waitpid(), or from the sys-
> tem() operator.
>
> For more info, man perlvar
>
> If you care to give it a test, here it goes:
>
> perl -e '`ls`;print $?'
> # prints "0" (hopefully)
>
> perl -e '`nonexistingcommand`;print $?'
> # prints "1" (unless you have some strange command on your machine...)
>
> HTH,
>
> jac
>
> On Thu, 2004-04-29 at 11:08, Durai wrote:
status.[color=darkred]
> --
> José Alves de Castro <jcastro@telbit.pt>
> Telbit - Tecnologias de Informação
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.665 / Virus Database: 428 - Release Date: 4/21/2004
| |
| Durai 2004-04-29, 10:09 am |
| Hi,
Thanks for help me.
Regs,
Durai.
----- Original Message -----
From: "Jose Alves de Castro" <jcastro@telbit.pt>
To: "Durai" <tech_durai@yahoo.com>
Cc: <beginners@perl.org>
Sent: Thursday, April 29, 2004 4:15 PM
Subject: Re: Running another program in current program
> First: you're missing a ';' (in the line where you're printing to the
> log file)
>
> Secondly: you're comparing $? with 0... now if I'm not mistaken, $? is 0
> if everything's ok, right? :-)
>
> So you probably want something like:
>
> #!/usr/bin/perl -w
> use strict;
>
> # ...
>
> open( FILE_LOG, ">log_file" );
>
> print FILE_LOG `perl $PRODUCT_DIR/tests/$MAIN_PRODUCTS_TESTS_STR/run`;
> if ($?) {
> print "Error";
> exit 0;
> }
>
>
>
> :-)
>
>
> On Thu, 2004-04-29 at 11:53, Durai wrote:
am[color=darkred]
> --
> José Alves de Castro <jcastro@telbit.pt>
> Telbit - Tecnologias de Informação
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.665 / Virus Database: 428 - Release Date: 4/21/2004
| |
| James Edward Gray II 2004-04-29, 4:51 pm |
| On Apr 29, 2004, at 5:08 AM, Durai wrote:
> Hello All,
>
> In shell script file, I can call another program & checked the exit
> status.
> Like:
>
> ./prog2
> if [ $? -ne 0 ]
> then
> echo "Error"
> exit
> fi
>
> How to do in perl?
Looks like you got your answer, but I want to add one more point.
Remember, Perl is a programming language, not a shell scripting
language. Perl doesn't need 50 little programs to do it's work. It
has it's own internal methods for solving those programs. More often
than not, shelling out is a mistake.
When you need to know how to do something The Perl Way(TM), just ask...
James
| |
| James Edward Gray II 2004-04-29, 4:51 pm |
| On Apr 29, 2004, at 5:53 AM, Durai wrote:
> Hi,
> I have written many test cases using shell script. But now
> I am
> writting in perl. I have done like the following one:
>
> open(FILE_LOG,">log_file");
Don't do this. When we ask the OS to do something for us, like open a
file, we need to make sure it succeeds, or find out why it didn't:
open LOG, 'log_file' or die "File error: $!";
James
|
|
|
|
|