Using END and the die function

Discussion in 'Perl Misc' started by pgodfrin, Feb 8, 2008.

  1. pgodfrin

    pgodfrin Guest

    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
     
    pgodfrin, Feb 8, 2008
    #1
    1. Advertising

  2. pgodfrin

    smallpond Guest

    On Feb 8, 2:07 pm, pgodfrin <> 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";
     
    smallpond, Feb 8, 2008
    #2
    1. Advertising

  3. On Feb 8, 2:07 pm, pgodfrin <> 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.
     
    nolo contendere, Feb 8, 2008
    #3
  4. pgodfrin

    pgodfrin Guest

    On Feb 8, 1:28 pm, nolo contendere <> wrote:
    > On Feb 8, 2:07 pm, pgodfrin <> 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
     
    pgodfrin, Feb 8, 2008
    #4
  5. pgodfrin <> wrote:
    > On Feb 8, 1:28 pm, nolo contendere <> wrote:
    >> On Feb 8, 2:07 pm, pgodfrin <> 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/"
     
    Tad J McClellan, Feb 8, 2008
    #5
  6. pgodfrin

    Ben Morrow Guest

    Quoth 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;
    > }


    {
    my $ex;

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

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

    Ben
     
    Ben Morrow, Feb 9, 2008
    #6
  7. pgodfrin

    pgodfrin Guest

    On Feb 8, 5:56 pm, Tad J McClellan <> wrote:
    > pgodfrin <> wrote:
    > > On Feb 8, 1:28 pm, nolo contendere <> wrote:
    > >> On Feb 8, 2:07 pm, pgodfrin <> 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
     
    pgodfrin, Feb 10, 2008
    #7
  8. Graham Drabble wrote:
    > On 08 Feb 2008 Tad J McClellan <> wrote in
    > news::
    >>
    >> 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
     
    Gunnar Hjalmarsson, Feb 10, 2008
    #8
  9. pgodfrin

    Ben Morrow Guest

    Quoth Gunnar Hjalmarsson <>:
    > Graham Drabble wrote:
    > > On 08 Feb 2008 Tad J McClellan <> wrote in
    > > news::
    > >>
    > >> 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
     
    Ben Morrow, Feb 11, 2008
    #9
  10. On Mon, 11 Feb 2008 00:10:06 +0100, Gunnar Hjalmarsson
    <> 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,
     
    Michele Dondi, Feb 11, 2008
    #10
  11. On 11 Feb 2008 Ben Morrow <> wrote in
    news::

    >
    > Quoth Gunnar Hjalmarsson <>:
    >> Graham Drabble wrote:
    >> > On 08 Feb 2008 Tad J McClellan <> wrote in
    >> > news::
    >> >>
    >> >> 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.


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


    --
    Graham Drabble
    http://www.drabble.me.uk/
     
    Graham Drabble, Feb 11, 2008
    #11
  12. pgodfrin

    pgodfrin Guest

    On Feb 11, 11:42 am, Graham Drabble <> wrote:
    > On 11 Feb 2008 Ben Morrow <> wrote innews::
    >
    >
    >
    > > Quoth Gunnar Hjalmarsson <>:
    > >> Graham Drabble wrote:
    > >> > On 08 Feb 2008 Tad J McClellan <> wrote in
    > >> >news::

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

    >
    > Good. Can't see any mention of that in the manual though. Any chance of
    > an addition to the man page?
    >
    > --
    > Graham Drabblehttp://www.drabble.me.uk/


    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
     
    pgodfrin, Feb 11, 2008
    #12
  13. pgodfrin

    pgodfrin Guest

    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
     
    pgodfrin, Feb 11, 2008
    #13
  14. pgodfrin

    Ben Morrow Guest

    Quoth 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;
    > }


    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
     
    Ben Morrow, Feb 11, 2008
    #14
  15. pgodfrin

    pgodfrin Guest

    On Feb 11, 3:47 pm, Ben Morrow <> wrote:
    > Quoth 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;
    > > }

    >
    > 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
     
    pgodfrin, Feb 13, 2008
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lloyd Sheen

    Die IDE Die

    Lloyd Sheen, Jan 24, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    434
    Natty Gur
    Jan 26, 2004
  2. Lloyd Sheen

    More Die IDE Die

    Lloyd Sheen, Jan 24, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    453
    Alvin Bruney
    Jan 25, 2004
  3. Replies:
    2
    Views:
    355
  4. Kyung won Cheon
    Replies:
    1
    Views:
    105
    Pit Capitain
    Aug 18, 2008
  5. Sara
    Replies:
    0
    Views:
    99
Loading...

Share This Page