Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

goto with anonymous subs

by kappa (Chaplain)
on Sep 12, 2005 at 15:03 UTC ( [id://491276]=perlmeditation: print w/replies, xml ) Need Help??

Hello, fellow monks!

Quite often we need to check some conditions right after subroutine entry. Maybe such pattern is actually too elementary and boring to be called a "pattern" in the Gang of Four sense. But let it be so for now. Some conditions are vital to continue, so unless they're met we just scream and return.

We usually do something like this:

sub really_big_form { my $q = shift; my $sid; unless ($sid = $q->param('session')) { print_error('Get a session, loser'); return; } unless ($session_vars{$sid}{new_style}) { respawn_session($sid); print_error('Old-style sessions are not cool, you are upgraded, go check your settings right now'); return; } # ... }

There're LOTS of ways (both as written practices and as ready CPAN modules) to reduce coding redundancy by creating some pre/post-condition lists and automatically evaluating them on subroutine entry. I've recently found out an interesting way to cut those returns. Maybe that's common knowledge, but I thought it will fit Meditation section.

sub really_big_form { my $q = shift; my $sid = $q->param('session') or goto sub { print_error('Get a session, loser'); }; $session_vars{$sid}{new_style} or goto sub { respawn_session($sid); print_error('Old-style sessions are not cool, you are upgraded, go check your settings right now'); }; # ... }

goto &sub is a well-known way of doing several certain things like calling newly-created subs from AUTOLOAD or jumping to "inherited" parent methods. It can also be used to "switch" to inline anonymous subs (or any coderef) as it turned out -- this is even a documented feature.

I've used goto $handler several times with dispatch tables but never goto sub {}. It looks funny (and therefore it's good) :)

--kap

Replies are listed 'Best First'.
Re: goto with anonymous subs
by merlyn (Sage) on Sep 12, 2005 at 15:25 UTC
    I don't see a gain here for goto sub { ... } over do { ...; return }. You still have the same size call-stack. I think I'd mark this down as being clever for clever's sake.

    Now, if you decided to implement continuation passing style with this:

    sub cool { my $return = pop; # get cps value my $p1 = shift; my $p2 = shift; ... ... if ($condition) { @_ = ($new, $arg, $new_return); goto &$return; } }
    Then I might be impressed.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      I'm not sure why you bring up call-stack size. Nothing in the OP leads me to think this is an attempt at optimization.

      The advantage over the do { ;return} form would be not having to have the return. This has three aspects: not having to type it in the first place, not having to read through it in the code, and not having to deal with what can be a debugging hassle of accidentally leaving it out.

      Looks to much like "GOSUB" for my tastes, though.

      It's even worse if we consider call stack. This trick clutters it with anonymous entry and therefore interferes with debugging. The only (arguable) gain is (a little) shorter code. I understand this.
      --kap
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: goto with anonymous subs
by dragonchild (Archbishop) on Sep 12, 2005 at 15:11 UTC
    This is slightly off the topic you're bringing up, but most CGI infrastructures, like CGI::Application and Catalyst provide a way to check these things before every request, in one place, in OO fashion. I suggest checking them out.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://491276]
Approved by itub
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-24 20:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found