Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Using END and the die function

Reply
Thread Tools

Using END and the die function

 
 
pgodfrin
Guest
Posts: n/a
 
      02-08-2008
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
 
Reply With Quote
 
 
 
 
smallpond
Guest
Posts: n/a
 
      02-08-2008
On Feb 8, 2:07 pm, pgodfrin <(E-Mail Removed)> wrote:
> 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";
 
Reply With Quote
 
 
 
 
nolo contendere
Guest
Posts: n/a
 
      02-08-2008
On Feb 8, 2:07*pm, pgodfrin <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
pgodfrin
Guest
Posts: n/a
 
      02-08-2008
On Feb 8, 1:28 pm, nolo contendere <(E-Mail Removed)> wrote:
> On Feb 8, 2:07 pm, pgodfrin <(E-Mail Removed)> wrote:
>
>
>
> > 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($?)
> > ...
> > 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.


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
 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      02-08-2008
pgodfrin <(E-Mail Removed)> wrote:
> On Feb 8, 1:28 pm, nolo contendere <(E-Mail Removed)> wrote:
>> On Feb 8, 2:07 pm, pgodfrin <(E-Mail Removed)> wrote:
>>
>>
>> > 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:


[ snip snippet ]

>> > 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";



>> 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.



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


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      02-09-2008

Quoth pgodfrin <(E-Mail Removed)>:
> 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

 
Reply With Quote
 
pgodfrin
Guest
Posts: n/a
 
      02-10-2008
On Feb 8, 5:56 pm, Tad J McClellan <(E-Mail Removed)> wrote:
> pgodfrin <(E-Mail Removed)> wrote:
> > On Feb 8, 1:28 pm, nolo contendere <(E-Mail Removed)> wrote:
> >> On Feb 8, 2:07 pm, pgodfrin <(E-Mail Removed)> wrote:

>
> >> > 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:

>
> [ snip snippet ]
>
> >> > 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";
> >> 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.

>
> 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
>
> --
> Tad McClellan
> email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


Yeah - I like that - thanks!
regards,
pg
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      02-10-2008
Graham Drabble wrote:
> On 08 Feb 2008 Tad J McClellan <(E-Mail Removed)> wrote in
> news:(E-Mail Removed):
>>
>> mkdir($tgt_dir) or warn "Mkdir ($tgt_dir) command failed.\n"
>> and exit 42;
>>
>> Reads like English:
>>
>> make the directory or warn and exit

>
> Can warn() ever return false?


Not AFAIK.

> If it did then your code above wouldn't exit.


To be safe, this is a possibility:

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

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      02-11-2008

Quoth Gunnar Hjalmarsson <(E-Mail Removed)>:
> Graham Drabble wrote:
> > On 08 Feb 2008 Tad J McClellan <(E-Mail Removed)> wrote in
> > news:(E-Mail Removed):
> >>
> >> mkdir($tgt_dir) or warn "Mkdir ($tgt_dir) command failed.\n"
> >> and exit 42;

> >
> > Can warn() ever return false?

>
> 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

 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      02-11-2008
On Mon, 11 Feb 2008 00:10:06 +0100, Gunnar Hjalmarsson
<(E-Mail Removed)> wrote:

>> Can warn() ever return false?

>
>Not AFAIK.
>
>> If it did then your code above wouldn't exit.

>
>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
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Die IDE Die Lloyd Sheen ASP .Net 2 01-26-2004 04:59 AM
More Die IDE Die Lloyd Sheen ASP .Net 5 01-25-2004 02:34 PM
DVD Verdict reviews: DIE! DIE! MY DARLING!, LOOK! PLAYFUL PATTERNS AND SIMPLE SHAPES / GO! EXERCISE WITH THE TELETUBBIES, and more! DVD Verdict DVD Video 0 09-26-2003 09:02 AM
I want to be a hacker/nerd b4 I die. Ghost issues (now and after I die) O.Phooey Computer Support 4 07-05-2003 08:35 PM



Advertisments