Click to See Complete Forum and Search --> : A different question about PERL return codes.


eXtremist
04-06-2001, 02:03 PM
Is there ANY way in PERL to make a program die with a return code of 0?

There are probably many ways to make a program EXIT with a return code of 0, but I need to use the command die. I know it sounds stupid, that I MUST use die, but I have to do at exactly as I were told (in my assignment).

YaRness
04-08-2001, 10:12 AM
try just making a script that dies, and calling it with a bash script or something and test the results (i.e. "if (script) foo else bar fi" or something). if you read the documentation on die ("perldoc -f die" or check the perlfunc man page), it uses the value of $! as the exit value when it dies.

eXtremist
04-09-2001, 05:54 AM
Yeah, I know die reads the $! value for its exit code, however if this is set to 0 then it chenges to something else.

Its almost as if die refuses to use 0 as its exit code. I was just hoping someone hacked it or found a way.

YaRness
04-09-2001, 08:05 AM
you could prolly wrap a shell script around it as a quick hack. what do you need it to return a 0 for?

Stuka
04-09-2001, 02:31 PM
eXtremist-
It makes perfect sense for die() to refuse to return 0. 0 is the OS code for normal program termination - certainly not what you'd expect w/die().

jemfinch
04-09-2001, 06:07 PM
Stick your entire program in an eval { BLOCK } and die when you want to exit. After the eval block, use exit(0).


#!/usr/bin/perl -w

eval {
print "Hey, I'm feeling depressed.\n";
print "I might be downright suicidal.\n";
die "I couldn't help myself";
}

exit(0);


Jeremy

eXtremist
04-10-2001, 11:36 AM
Originally posted by YaRness:
<STRONG>you could prolly wrap a shell script around it as a quick hack. what do you need it to return a 0 for?</STRONG>

Okay.. Well, you see, I'm doing CompSci. in university, and each week we're given a programming assignment to complete. We have to do it as specified, so there's not much I can do. I was supposed to use 'die' to quit the program on an error.

Now, we also have a program called test-asn to test our programs after they're coded. Most of the marks come from this test harness. The test program checks STDOUT, STDERR, and the return code of the program.
Now, when I test my program with this harness, I get errors about my return code. The program tells me they should all be 0, but I haven't figured out how to do this using die.

My prof. assured me that it is possible, without using the exit(0) command. So I dunno. I thought I'd throw it out here hoping someone would know. It's not documented, as far as I can tell, so it's probably some strange hack or something.

Anyways, I did replace all my die's with exit's, but I'm probably gonna lose marks for that. :(

kmj
04-10-2001, 11:58 AM
perhaps this means you shouldn't be returning an error for that specific test input? Is that possible?

YaRness
04-10-2001, 12:53 PM
you may also wish to post your questions at perlmonks.org, there are people there that know a HELLUVA lot about perl.

and no putzes telling you to switch to X language either :D

eXtremist
04-11-2001, 10:21 AM
Originally posted by YaRness:
<STRONG>and no putzes telling you to switch to X language either :D</STRONG>

Hehe.. Yah.. I love suggestions that will help me be a better programmer, such as "give x language a try.. it's cool".

But when I ask a perl specific question, it bugs the hell outta me when I get the reply "use python".

uGh!!!!!! :)

YaRness
04-11-2001, 10:28 AM
Originally posted by eXtremist:
<STRONG>Hehe.. Yah.. I love suggestions that will help me be a better programmer, such as "give x language a try.. it's cool".

But when I ask a perl specific question, it bugs the hell outta me when I get the reply "use python".

uGh!!!!!! :)</STRONG>

ditto. there's advocacy, and then there's being an arse, among other things.

kmj
04-11-2001, 11:20 AM
Originally posted by YaRness:
<STRONG>ditto. there's advocacy, and then there's being an arse, among other things.</STRONG>

I don't see that in this thread; in fact jemfinch (the ultimate python advocate) didn't even mention python. He actually gave some of the most useful perl advice.

YaRness
04-11-2001, 11:31 AM
Originally posted by kmj:
<STRONG>I don't see that in this thread</STRONG>
i wasn't referring to this thread.
Originally posted by kmj:
<STRONG>He actually gave some of the most useful perl advice.</STRONG>
as i've said before (you can search the forum yerself), when finch does give a perl response, he knows what the hell he's talking about. and sometimes even does it politely.

and last i checked was also fairly capable of defending his own self.

kmj
04-11-2001, 02:12 PM
and last i checked was also fairly capable of defending his own self.

okay, I'll shut up....

TheLinuxDuck
04-11-2001, 02:32 PM
How about just simply undef'ing $! before die is called? Like:

#!/usr/bin/perl -w
use strict;

if(open IN, "some_non_existant_file") {
print "We weren't supposed to find a file of that name...\n";
close IN;
}
else {
undef $!;
die "We died, man: $!\n";
}


According to PP3rdEd, when $! is 0, die returns the value of $? right-shifted by 8. And, if $? &gt;&gt; 8 is 0, then a value of 255 is returned, but it doesn't specify what happens when $! is undefined. :)

I'm curious to know if that works for you.