Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Re: Double your return!!!!

by pfaut (Priest)
on Feb 19, 2003 at 15:01 UTC ( [id://236655]=note: print w/replies, xml ) Need Help??


in reply to Re: Double your return!!!!
in thread Double your return!!!!

A called routine should not make any assumptions about the routine that called it, or the routine that called that routine, etc. As stated in the base node, if you really want to do this, the proper way would be some kind of exception signalling mechanism.

Here's your little snippet.

sub bar { $foo = askFor( title => 'Name:' ); return undef if not defined $foo; $baz = askFor( title => 'Cigarettes:' ); return undef if not defined $baz; }

Now, I doubt very much that the whole point of this subroutine was to ask for a couple of pieces of information and throw them away. So, there would probably be some code after the second return that actually does something with $foo and $baz.

Suppose there were a couple of other places in your program that needed to ask for these pieces of information. Instead of having those four lines all over your code, you decide to refactor and make a separate subroutine called getFooAndBaz() to ask for those values. Does askFor() now have to jump three call frames? Why should it care what path was taken to call it? How does that affect the procedure that calls askFor() directly?

--- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';

Replies are listed 'Best First'.
Re:x3 Double your return!!!!
by bsb (Priest) on Feb 20, 2003 at 07:46 UTC
    I wasn't seriously suggesting this as useful code.

    I intended the question more as a perl puzzle that I couldn't solve. The code was only to illustrate the question and the critical text was is it possible?

    Reading the responses and re-reading my initial post I obviously didn't make my intention clear enough.

    Sometimes though, a joke or japh ends up being useful.

    I don't think Quantum::Superpositions was intended seriously yet junctions (or whatever the name is now) is going to be in perl6.

      Not possible in perl5 AFAIK.

      Since parrot is supposed to support continuations something like this might be possible in perl6 I guess.

      I know of one language that supports the idiom. Pop-11 has chainfrom, chainto, exitto and exitfrom which cover the full gamut of exit variations. However, apart from using chainto to optimise tail recursion I've never used them in "real" code :-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (None)
    As of 2024-04-25 01:03 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found