Home > Archive > PERL Beginners > December 2007 > Perl loop
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]
|
|
| manojkumarg@dataone.in 2007-12-14, 8:00 am |
| Hello List,
I am having two conditions in the perl program. after the check it will be regenerated to a valid or notvalid file depending on the check.
Checking " if (system($touchcmd) && system($chkstat)) " as condition. Is this a valid one? also was trying to get this done buy calling subroutine.
My check are like this..
this was the initial one ...wanted to remove the second if...
if(CondA) {
if(CondB) {
#write to valid one }
else {
#write to notvalid file with different flag }
}
else {
# write to notvalid file with error flag }
This is the scratch book ...know its messy...and both CondA and CondB are system commands (UNIX).
#!/bin/perl
$interfaces="Output - working interface";
$chkstat="cat tmp";
$touchcmd="touch test";
if (system($touchcmd) && system($chkstat)) {
print $?."\tyes stmt";
`echo $interfaces >>outfile.txt`;
}
else {
print $?."\tNo stmt";
`echo $_ >>notdone.txt`;
}
Is it good practice to check for $? in program? I haven't seen anybody using that.
#sub testa {
# system($chkstat);
# $?;
#}
#sub testb {
# system($touchcmd);
# $?;
#}
Thanks
Manoj
| |
| Jeff Pang 2007-12-14, 8:00 am |
|
--- manojkumarg@dataone.in wrote:
>
> Checking " if (system($touchcmd) && system($chkstat)) " as
> condition. Is this a valid one? also was trying to get this done
> buy calling subroutine.
>
No. Generally when an unix command was executed successfully, it will
return 0. ie,
$ perl -le '$s = system("ls -l >/dev/null");print $s'
0
so,you'd better use the condition with $? variable (though it's not
absolute),
$ perl -le 'system("ls -l >/dev/null");if ($?==0) {print "true"}'
true
Best Regards,
Jeff (joy) Peng
________________________________________
________________________________________
____
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06...tDypao8Wcj9tAcJ
| |
| manojkumarg@dataone.in 2007-12-14, 7:02 pm |
| Thanks Jeff.
How will this be considered.
if(system(CondA) && system(CondB)) ...
I am getting 0 as the $? RC...but goes to the else part of the if-statement. Both the conditions (Unix commands are executed). Is it taking the CondB RC?
Thanks
Manoj
if(
----- Original Message -----
From: jeff pang <joy_peng@yahoo.com>
Date: Friday, December 14, 2007 7:09 pm
Subject: Re: Perl loop
To: manojkumarg@dataone.in, "beginners@perl.org" <beginners@perl.org>
> --- manojkumarg@dataone.in wrote:
>
>
> No. Generally when an unix command was executed successfully, it will
> return 0. ie,
>
> $ perl -le '$s = system("ls -l >/dev/null");print $s'
> 0
>
> so,you'd better use the condition with $? variable (though it's not
> absolute),
>
> $ perl -le 'system("ls -l >/dev/null");if ($?==0) {print "true"}'
> true
>
> Best Regards,
> Jeff (joy) Peng
>
>
> ________________________________________
________________________________________
____
> Be a better friend, newshound, and
> know-it-all with Yahoo! Mobile. Try it now.
> http://mobile.yahoo.com/;_ylt=Ahu06...tDypao8Wcj9tAcJ
>
>
| |
| Jeff Pang 2007-12-14, 7:02 pm |
|
--- manojkumarg@dataone.in wrote:
> Thanks Jeff.
>
> How will this be considered.
>
> if(system(CondA) && system(CondB)) ...
>
I'd better write it as:
system "cronA && cronB";
if ($? == 0 ) {
do something...
}
Best Regards,
Jeff (joy) Peng
________________________________________
________________________________________
____
Looking for last minute shopping deals?
Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newse...tegory=shopping
| |
| manojkumarg@dataone.in 2007-12-14, 7:02 pm |
|
Digging on this....to make it work.
Thanks again.
----- Original Message -----
From: jeff pang <joy_peng@yahoo.com>
Date: Friday, December 14, 2007 7:45 pm
Subject: Re: Perl loop
To: manojkumarg@dataone.in
Cc: "beginners@perl.org" <beginners@perl.org>
> --- manojkumarg@dataone.in wrote:
>
>
> I'd better write it as:
>
> system "cronA && cronB";
> if ($? == 0 ) {
> do something...
> }
>
> Best Regards,
> Jeff (joy) Peng
>
>
> ________________________________________
________________________________________
____
> Looking for last minute shopping deals?
> Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newse...tegory=shopping
>
| |
| John W . Krahn 2007-12-14, 7:02 pm |
| On Friday 14 December 2007 05:31, manojkumarg@dataone.in wrote:
>
> Hello List,
Hello,
> I am having two conditions in the perl program. after the check it
> will be regenerated to a valid or notvalid file depending on the
> check.
>
> Checking " if (system($touchcmd) && system($chkstat)) " as condition.
> Is this a valid one?
No, you would have to do:
if ( !system($touchcmd) && !system($chkstat) )
Perhaps $touchcmd and $chkstat could be done in perl instead of running
them in an external process? What exactly do $touchcmd and $chkstat do?
John
--
use Perl;
program
fulfillment
| |
| Martin Barth 2007-12-14, 7:02 pm |
| Hi
> Perhaps $touchcmd and $chkstat could be done in perl instead of running
> them in an external process? What exactly do $touchcmd and $chkstat do?
$chkstat="cat tmp";
The source looks to me that you only want to see if the file is created or not?
you can simply call open(); to check this for you, but stat() would be better, i think.
$touchcmd="touch test";
for touch you can use
http://search.cpan.org/~nwetters/Fi...h-0.02/Touch.pm
If you dont have File::Touch installed, the main thing is
sysopen my $fh,$file,O_WRONLY|O_CREAT|O_NONBLOCK|O_
NOCTTY or
croak("Can't create $file : $!");
close $fh or croak("Can't close
$file : $!");
if your sysopen() works, your file is surely created.
I think that would make the hole thing faster and easier.,
| |
| John W . Krahn 2007-12-14, 7:02 pm |
| On Friday 14 December 2007 09:29, Martin Barth wrote:
>
> Hi
Hello,
>
> $chkstat="cat tmp";
>
> The source looks to me that you only want to see if the file is
> created or not? you can simply call open(); to check this for you,
> but stat() would be better, i think.
>
>
> $touchcmd="touch test";
Since you are not the OP then how do you know what $touchcmd and
$chkstat do?
John
--
use Perl;
program
fulfillment
| |
| Martin Barth 2007-12-14, 7:02 pm |
| tat do?
sorry john,
what do you mean by that? :(
I took the code:
#!/bin/perl
$interfaces="Output - working interface";
$chkstat="cat tmp";
$touchcmd="touch test";
if (system($touchcmd) && system($chkstat)) {
from the first e-mail
| |
| John W . Krahn 2007-12-14, 7:02 pm |
| On Friday 14 December 2007 11:55, Martin Barth wrote:
>
> tat do?
>
> sorry john,
> what do you mean by that? :(
>
> I took the code:
>
> #!/bin/perl
>
> $interfaces="Output - working interface";
> $chkstat="cat tmp";
> $touchcmd="touch test";
> if (system($touchcmd) && system($chkstat)) {
>
> from the first e-mail
Sorry Martin, I didn't read past the first two paragraphs that I
responded to. :(
Anyway, it looked like the OP was only posting simple examples and not
real working code (if that helps any) which makes it hard to actually
help. :-)
John
--
use Perl;
program
fulfillment
| |
| Jeff Pang 2007-12-14, 10:02 pm |
|
--- "John W.Krahn" <krahnj@telus.net> wrote:
>
> No, you would have to do:
>
> if ( !system($touchcmd) && !system($chkstat) )
>
I have thought that, if the unix command's author doesn't return a 0
from the code, how will we get the result of a system command was
executed successfully or not?
ie, maybe the ppl can return 1 or any other value except 0 from the
code:
$ cat test.c
#include <stdio.h>
main () {
printf("hello world");
return 1;
}
When I complied it and run it:
$ ./test
hello world
then get the value of $?:
$ echo $?
1
It's 1 not 0.
How do you think about this case? :)
Best Regards,
Jeff (joy) Peng
________________________________________
________________________________________
____
Never miss a thing. Make Yahoo your home page.
http://www.yahoo.com/r/hs
| |
| Chas. Owens 2007-12-15, 4:01 am |
| On Dec 14, 2007 10:01 PM, jeff pang <joy_peng@yahoo.com> wrote:
snip
> When I complied it and run it:
>
> $ ./test
> hello world
>
> then get the value of $?:
>
> $ echo $?
> 1
>
> It's 1 not 0.
> How do you think about this case? :)
snip
If it is in-house, I would find the culprit and beat him or her for a
little while with Advanced Programming in the UNIX Environment (it is
about two inches thick and weighs two to three pounds). If it is
third-party software, I would either rewrite the functionality in
Perl, write a wrapper script, or just document the heck out of that
line of code. My biggest concern would be what else is wrong with the
program if the author knew so little about UNIX programming that he or
she used a non-zero exit code for success. Also note that $? in Perl
is not the same as $? in shell. In shell it is the exit code; in Perl
it is the exit code (high byte) and the exec return value (low byte).
See $?'s entry in perldoc perlvar* or perldoc -f system** for more
information.
* http://perldoc.perl.org/perlvar.html#%24%3f
** http://perldoc.perl.org/functions/system.html
| |
| John W . Krahn 2007-12-15, 4:01 am |
| On Friday 14 December 2007 19:01, jeff pang wrote:
>
> --- "John W.Krahn" <krahnj@telus.net> wrote:
>
> I have thought that, if the unix command's author doesn't return a 0
> from the code, how will we get the result of a system command was
> executed successfully or not?
>
> ie, maybe the ppl can return 1 or any other value except 0 from the
> code:
Then that would go against either the "Portable Operating System
Interface" (POSIX) standard[1] or the Single UNIX Specification (SUS)
standard[2]. Even MS-DOS and MS Windows batch files follow the same
convention where 0 is success and not-0 is failure.
> $ cat test.c
> #include <stdio.h>
>
> main () {
In C the main function returns an int so that is not compliant with the
C standard[3]. It should be:
int main ( void ) {
> printf("hello world");
That may not print anything as stdout is buffered. You need to end the
string with a newline to ensure that the buffer is flushed:
printf( "hello world\n" );
> return 1;
Rather than using "magic numbers" you should use the EXIT_SUCCESS or
EXIT_FAILURE macros defined in the stdlib.h[4] header file:
cat /usr/include/stdlib.h
[ SNIP ]
/* We define these the same for all machines.
Changes from this to the outside world should be done in `_exit'. */
#define EXIT_FAILURE 1 /* Failing exit status. */
#define EXIT_SUCCESS 0 /* Successful exit status. */
> }
> When I complied it and run it:
>
> $ ./test
> hello world
>
> then get the value of $?:
>
> $ echo $?
> 1
>
> It's 1 not 0.
> How do you think about this case? :)
I think that anyone with an idea in their head can make a case for any
absurd notion but that does not make it right or standards compliant.
1) http://en.wikipedia.org/wiki/POSIX
2) http://en.wikipedia.org/wiki/Single_UNIX_Specification
http://www.unix.org/version3/
3) http://en.wikipedia.org/wiki/C_(programming_language)
4) http://en.wikipedia.org/wiki/Stdlib.h
John
--
use Perl;
program
fulfillment
| |
| John W . Krahn 2007-12-15, 4:01 am |
| On Friday 14 December 2007 19:01, jeff pang wrote:
>
> --- "John W.Krahn" <krahnj@telus.net> wrote:
>
> I have thought that, if the unix command's author doesn't return a 0
> from the code, how will we get the result of a system command was
> executed successfully or not?
>
> ie, maybe the ppl can return 1 or any other value except 0 from the
> code:
And another point, perhaps the main point, is that a program could have
many different reasons for failure. With 0 for success and any
non-zero value for failure you can specify exactly why the program
failed by returning any positive integer for each failure reason.
If you inverse that then you could only have ONE reason to fail and
MANY different reasons for success. Do you really want that? Think
about it.
John
--
use Perl;
program
fulfillment
| |
| Chas. Owens 2007-12-15, 7:00 pm |
| On Dec 15, 2007 2:50 AM, John W. Krahn <krahnj@telus.net> wrote:
snip
>
> In C the main function returns an int so that is not compliant with the
> C standard[3]. It should be:
>
> int main ( void ) {
>
snip
That depends on the version of C you are using. Given the lack of
other syntax in this short program I cannot tell if it is K&R C, ANSI
C, or C99. If he is using K&R C, that is a perfectly fine main
definition (int return type is assumed for functions that don't
specify). The whole putting a void in the argument list thing came
from C++; it is still perfectly valid (from K&R to C99) to define your
main like this
int main () {
snip[color=darkred]
> I think that anyone with an idea in their head can make a case for any
> absurd notion but that does not make it right or standards compliant.
snip
I agree with this whole heartedly. All versions of C are loaded guns.
Anyone can pick one up, but that doesn't mean I want to stand next to
that person. But I think Jeff's main point was, since it is possible,
how should we handle it? My suggestion is to do physical harm to the
person if you can find him or her until he or she agrees to stop being
an idiot. If you can't find the person responsible for it, then avoid
the software (because if it gets this fundamental thing wrong, what
else is wrong with it). And as a last case (you can't find the person
and you have to use the software), write a wrapper script* that
corrects this issue and document the heck out of the line where you do
the system** call.
* something like this (warning untested)
#!/bin/sh
/path/to/broken/prog/i_am_borked
rc=$?
case $rc in
0) exit 1;;
1) exit 0;;
*) exit $rc;;
esac
** Doing a system call at all is a red flag. It is rarely needed
unless you are using Perl to puppet string** another process, and even
then IPC::Open3 is often a better tool.
*** that is the purpose of your script is to take the place of a human
interacting with the system
| |
| John W . Krahn 2007-12-15, 7:00 pm |
| On Saturday 15 December 2007 06:17, Chas. Owens wrote:
>
> On Dec 15, 2007 2:50 AM, John W. Krahn <krahnj@telus.net> wrote:
>
>
> snip
>
> That depends on the version of C you are using. Given the lack of
> other syntax in this short program I cannot tell if it is K&R C, ANSI
> C, or C99. If he is using K&R C, that is a perfectly fine main
> definition (int return type is assumed for functions that don't
> specify).
I *DARE* you ... no, wait ... I DOUBLE DAWG DARE YOU to post that
opinion on comp.lang.c and/or comp.programming. Go on, I'll wait. :-)
> The whole putting a void in the argument list thing came
> from C++; it is still perfectly valid (from K&R to C99) to define
> your main like this
>
> int main () {
>
> snip
>
>
> snip
>
> I agree with this whole heartedly. All versions of C are loaded
> guns. Anyone can pick one up, but that doesn't mean I want to stand
> next to that person. But I think Jeff's main point was, since it is
> possible, how should we handle it?
Why don't we all just ignore standards, join the Microsoft Borg and do
anything we want? (Vote for Ron Paul!!!!) Since anything is possible
why don't we just ignore anything the program has to say? How do we
even know that the "data" it spews out is valid?
;-)
John
--
use Perl;
program
fulfillment
| |
| Chas. Owens 2007-12-15, 7:00 pm |
| On Dec 15, 2007 12:22 PM, John W. Krahn <krahnj@telus.net> wrote:
snip
>
> I *DARE* you ... no, wait ... I DOUBLE DAWG DARE YOU to post that
> opinion on comp.lang.c and/or comp.programming. Go on, I'll wait. :-)
snip
That would be foolhardy indeed since comp.lang.c is an ANSI/ISO/C99
newsgroup. Posting K&R C statements and questions to it would be
roughly equivalent to posting about Perl 4 here.
snip
>
> Why don't we all just ignore standards, join the Microsoft Borg and do
> anything we want? (Vote for Ron Paul!!!!) Since anything is possible
> why don't we just ignore anything the program has to say? How do we
> even know that the "data" it spews out is valid?
>
> ;-)
snip
Well, yes, we have to deal with bad, buggy, standards violating
software everyday. The question isn't "should we violate standards",
it is "what should our response be". Mine is (metaphorical) violence
aimed at the author of the bad program if he or she can be found,
rewriting the offending code if he or she can't be found, and
workarounds when they can't found and we are still forced to use it.
Let me give you a scenario:
You are a contractor at a firm. The company has just bought FooBar
version 2007.99.1. It is a piece of hardware that plugs into the USB
port of a server and provides ISO FLECIDOODLE compliance. You are
tasked to port some of their code to use this new FLECIDOODLE
compliant hardware. In order to initialize the system for a
transaction the manual says you must run "/usr/bin/foobar start task".
The foobar program returns 0 on failure, -1 on warnings, and a 1 on
success. On success or warnings, it writes a transaction id to
STDOUT. On warning or failure it writes one or more messages to
STDERR. To close a transaction you must call "/usr/bin/foobar end
$transactionid". After snooping on the data the program sends to the
device, it appears as if the communication is encrypted. There are
currently no modles on CPAN that deal with FooBar 2007.99.1 or
FLECIDOODLE. Your boss wants a solution by the end of the w , what
do you do?
A few (but not necessarily all) options are
1. whine and moan about how the return codes are not POSIX/SUS compliant
2. attempt to reverse-engineer FooBar version 2007.99.1's
communication protocol and hope that once you have done that it won't
change in a future release and that your implementation is good enough
for FLECIDOODLE compliance (i.e. foobar isn't doing something you
don't know about from the manual)
3. Purchase the FLECIDOODLE spec and try to implement a compliant Perl
version from scratch
4. Quit
5. Write a quick Perl module for the other code to use and document
the heck out it
Personally, I would do number 5 and start investigating number 3
without much hope (once a company has bought bad software they tend to
be dead set on using it).
|
|
|
|
|