Using END and the die function

P

pgodfrin

Greetings,
I'd like to die() with a return code (no pun intended) - I'm not sure
how to do it without wrapping some extra code around die. Any
thoughts?

Here's a snippet:
if($?) { $return_code=$?; die "Error generating temp file names\n" ;}
....
END
{
if (defined WL) { close WL; }
print "Script ended. RC=$return_code \n";
$?=$return_code;
}

This works fine if I explicitly code both steps - first setting the
$return_code and then executing die. But, this kind of statement won't
permit that 'cause of the nature of the or operator:

mkdir($tgt_dir) or die "Mkdir ($tgt_dir) command failed.\n";

Is there an elegant way to this or will I need to use if statements
for anything I want to test for errors?
pg
 
S

smallpond

Greetings,
I'd like to die() with a return code (no pun intended) - I'm not sure
how to do it without wrapping some extra code around die. Any
thoughts?

Here's a snippet:
if($?) { $return_code=$?; die "Error generating temp file names\n" ;}
...
END
{
if (defined WL) { close WL; }
print "Script ended. RC=$return_code \n";
$?=$return_code;

}

This works fine if I explicitly code both steps - first setting the
$return_code and then executing die. But, this kind of statement won't
permit that 'cause of the nature of the or operator:

mkdir($tgt_dir) or die "Mkdir ($tgt_dir) command failed.\n";

Is there an elegant way to this or will I need to use if statements
for anything I want to test for errors?
pg

how about:

sub mydie {
$!= shift;
die @_;
}

mkdir($tgt_dir) or mydie 3,"Mkdir ($tgt_dir) command failed.\n";
 
N

nolo contendere

Greetings,
I'd like to die() with a return code (no pun intended) - I'm not sure
how to do it without wrapping some extra code around die. Any
thoughts?

Here's a snippet:
if($?) { $return_code=$?; die "Error generating temp file names\n" ;}
...
END
{
if (defined WL) { close WL; }
print "Script ended. RC=$return_code \n";
$?=$return_code;

}

This works fine if I explicitly code both steps - first setting the
$return_code and then executing die. But, this kind of statement won't
permit that 'cause of the nature of the or operator:

mkdir($tgt_dir) or die "Mkdir ($tgt_dir) command failed.\n";

Is there an elegant way to this or will I need to use if statements
for anything I want to test for errors?

Would 'exit' suit your needs? Just 'warn' whatever message you want,
then call 'exit' with the return code you want.
 
P

pgodfrin

Would 'exit' suit your needs? Just 'warn' whatever message you want,
then call 'exit' with the return code you want.

That would work, but it's still a two step operation, so I couldn't
use the or operator. using a "mydie" function is just a wrapper and
while that would work too, I'm seeking a more 'stylish' or elegant way
to do it. Frankly I could just do: if (!mkdir($tgt_dir))
{ $return_code=$?; die "Error generating temp file names\n" ;}.

But I'm feeling a bit whiney and thought there might be a 'cleaner
way'.
pg
 
T

Tad J McClellan

[ snip snippet ]
That would work, but it's still a two step operation, so I couldn't
use the or operator.


Sure you could:

mkdir($tgt_dir) or warn "Mkdir ($tgt_dir) command failed.\n" and exit 42;

Reads like English:

make the directory or warn and exit
 
B

Ben Morrow

Quoth pgodfrin said:
Greetings,
I'd like to die() with a return code (no pun intended) - I'm not sure
how to do it without wrapping some extra code around die. Any
thoughts?

Here's a snippet:
if($?) { $return_code=$?; die "Error generating temp file names\n" ;}
...
END
{
if (defined WL) { close WL; }
print "Script ended. RC=$return_code \n";
$?=$return_code;
}

{
my $ex;

sub my_die {
$ex = $_[0];
die $_[1];
}

END { $ex and $? = $ex }
}

Ben
 
P

pgodfrin

[ snip snippet ]
That would work, but it's still a two step operation, so I couldn't
use the or operator.

Sure you could:

mkdir($tgt_dir) or warn "Mkdir ($tgt_dir) command failed.\n" and exit 42;

Reads like English:

make the directory or warn and exit

Yeah - I like that - thanks!
regards,
pg
 
B

Ben Morrow

Quoth Gunnar Hjalmarsson said:
Not AFAIK.

Make that 'no' :). The warn op unconditionally returns true. It may die,
but in that case the exit doesn't get called anyway.

Ben
 
M

Michele Dondi

Not AFAIK.


To be safe, this is a possibility:

mkdir($tgt_dir) or
do { warn "Mkdir ($tgt_dir) command failed.\n"; exit 42 };

Sometimes, just for consistency, I'd like a low precedence (scalar)
comma. Like, if it were C<then>:

mkdir $tgt_dir or
warn "Mkdir ($tgt_dir) command failed.\n" then
exit 42;


Michele
 
G

Graham Drabble

Make that 'no' :). The warn op unconditionally returns true.

Good. Can't see any mention of that in the manual though. Any chance of
an addition to the man page?
 
P

pgodfrin

Good. Can't see any mention of that in the manual though. Any chance of
an addition to the man page?

A man after my own heart. In fact, why is it often guesswork in seeing
what the arguments are to a function and what the possible return
values are?

btw - this works effectively but not elegantly:

mkdir($tgt_bkp) or warn aud_dt, "Mkdir ($tgt_bkp) command failed.
\n" and $return_code=99 and exit;

Funny - all I'm trying to do is make this program communicate with the
outside world that something failed, but I wonder if using and END
block is messing me up hmmm...
pg
 
P

pgodfrin

Well - over twenty years in this business and I still find ways to be
an idiot. What I did not include in my previous posts was that in my
END subroutine I had an extra call - I didn't think it was important
(duh!). Of course it is. So by 'trapping' the $? before processing my
END code, things actually work as intended:

mkdir($tgt_dir) or die "Mkdir ($tgt_dir) command failed.\n";
END
{
$rc=$? ; # trap $? 'child error'
print `date`,"Script ended. RC=$rc \n";
$?=$rc;
}

The `date` was setting the $? variable, so the the die function worked
but I was self-immolating in my END routine...
pg
 
B

Ben Morrow

Quoth pgodfrin said:
Well - over twenty years in this business and I still find ways to be
an idiot. What I did not include in my previous posts was that in my
END subroutine I had an extra call - I didn't think it was important
(duh!). Of course it is. So by 'trapping' the $? before processing my
END code, things actually work as intended:

mkdir($tgt_dir) or die "Mkdir ($tgt_dir) command failed.\n";
END
{
$rc=$? ; # trap $? 'child error'
print `date`,"Script ended. RC=$rc \n";
$?=$rc;
}

A better way to do this is

END {
my $rc = $?;
local $?;
print `date`, "Script ended. RC=$rc\n";
}

as then perl will restore $? for you. Also, you *really* don't need to
call date(1) just to print the date. If you just want the default
ctime(3) output, you can use

print scalar localtime, "...";

otherwise, use POSIX::strftime to format the date as you like.

Ben
 
P

pgodfrin

A better way to do this is

END {
my $rc = $?;
local $?;
print `date`, "Script ended. RC=$rc\n";
}

as then perl will restore $? for you. Also, you *really* don't need to
call date(1) just to print the date. If you just want the default
ctime(3) output, you can use

print scalar localtime, "...";

otherwise, use POSIX::strftime to format the date as you like.

Ben

cool! I'll give it a try...
tanx,
pg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top