Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Goto-labels for exception handling?

by RonW (Parson)
on May 10, 2018 at 00:55 UTC ( #1214304=note: print w/replies, xml ) Need Help??


in reply to Goto-labels for exception handling?

Using goto to get out of a subroutine is not something I've seen before - except in BASIC, where doing so leaves a mess on the call stack. And in assembly, where it also leaves a mess unless the coder explicitly cleans up the stack.

While it is true that using goto can avoid deeply nested conditionals, it's generally better to set an error variable and use last:

my $error; { ...; $error = 'message', last if ( $foo == $bar ); ...; } if (defined $error) { ...; # clean up mess and/or warn the user }

And your code is using a variable behind the scene, so this just brings the variable into the foreground.

But, if you really don't like that, you could try:

{ do { ...; $foo == $bar and warn 'foo bar problem'; } or do { ...; } or do { last; } ...; # clean up mess }

(I think warn will return a non-0 value. If not, does print?)

Replies are listed 'Best First'.
Re^2: Goto-labels for exception handling?
by LanX (Archbishop) on May 10, 2018 at 01:00 UTC
    > Using goto to get out of a subroutine is not something I've seen before

    that's supported in Perl, the call stacks are properly cleaned. *

    > a variable behind the scene, 

    The closure variable?

    For the rest I'm not sure what you are trying to tell me. (Probably not to use exceptions at all for errors? )

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Wikisyntax for the Monastery

    *) from goto : it can be used to go almost anywhere else within the dynamic scope, including out of subroutines ( but it's usually better to use some other construct such as last or die.)

      For the rest I'm not sure what you are trying to tell me. (Probably not to use exceptions at all for errors? )

      I was presuming you want to avoid using die/eval for exception handling.

      Anyway, it is an interesting approach. But it is basically a shorthand for $errmsg = "message", goto ERRLABEL

      Shorthand is fine. In this case, however, I think it hides something that the coder using your approach needs to be very aware of: That it is goto. The biggest problem with that being the dynamic scope. If the coder forgets this, the coder will discover the code's control flow jumping to unexpected places.

        > The biggest problem with that being the dynamic scope. If the coder forgets this, the coder will discover the code's control flow jumping to unexpected places.

        Exactly and thank you for getting to the point.

        Contrary to die I can react to the case of a missing "receptor".

        But what if this supposedly missing labeled was already used somewhere up the dynamic scope?

        I think this could be solved... but I wanted to see if this wheel was already invented before.

        Unfortunately does already the mention of "goto" cause a lot of unrelated emotions ... :/

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Wikisyntax for the Monastery

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1214304]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2019-04-19 02:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I am most likely to install a new module from CPAN if:
















    Results (106 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!