Re: Double your return!!!!
by dws (Chancellor) on Feb 19, 2003 at 04:24 UTC
|
| [reply] |
Re: Double your return!!!!
by tachyon (Chancellor) on Feb 19, 2003 at 04:25 UTC
|
print &zero, marine();
sub zero { 'red ' }
sub marine { $tom_clancy = 'october' }
A double return makes no logical sense to me.
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
| [reply] [d/l] |
|
|
sub zero { 'Chinese ninja warrior ' }
See also CHI-nese Ninja WAR-rrior.
--
F
o
x
t
r
o
t
U
n
i
f
o
r
m
Found a typo in this node? /msg me
The hell with paco, vote for Erudil!
| [reply] [d/l] |
|
|
| [reply] |
|
|
| [reply] |
Re: Double your return!!!!
by steves (Curate) on Feb 19, 2003 at 05:11 UTC
|
There's a paper on continuations here. It mentions Scheme, which is where I recall hearing of these sorts of things. I haven't programmed Scheme, but my rather weak understanding of continuations is that they're similar to C's setjmp/longjmp except that you can longjmp back to a location even if it's in a function that's already returned. I think Forth also has the ability to alter the calling stack in a way that does this sort of thing but I may be wrong.
Here's another interesting read that should make your head hurt a little.
| [reply] |
|
|
FORTH can do it indeed, by manipulating the return stack directly. For example:
: foo ( -- ) ." enter foo" cr rdrop exit ." exit foo" cr ;
: bar ( -- ) ." enter bar" cr foo ." exit bar" cr ;
: baz ( -- ) ." enter baz" cr bar ." exit baz" cr ;
gives the output:
enter baz
enter bar
enter foo
exit baz
If your forth doesn't have rdrop, use r> drop or define it with: : rdrop r> r> drop >r ;
I also strongly recall having seen words specifically for this purpose, but I can't find one in this forth (maybe they left it out in modern implementations) | [reply] [d/l] [select] |
|
|
The ;=) operator should've given it away.
| [reply] |
Re: Double your return!!!!
by sauoq (Abbot) on Feb 19, 2003 at 04:50 UTC
|
I think the example you give is a fine way to handle it.
Actually allowing a called subroutine to force its caller to return could make for some pretty unmaintable code. Needing that would probably indicate an opportunity to improve the design.
Other than for deliberate obfuscation, I can't think of one good reason why someone would want such a thing. Can you provide one?
-sauoq
"My two cents aren't worth a dime.";
| [reply] |
|
|
No.
I can think of uses but, as you say, wishing for a double
return is probably a red-flag.
I just like knowing what Perl can do and can't do.
| [reply] |
Re: Double your return!!!!
by BrowserUk (Patriarch) on Feb 19, 2003 at 05:16 UTC
|
You don't say why you'd want to do this?
In most cases sub x{ ....; return 1 if $ok; return; }
could be replaced by sub x{ ...; $ok;}
And I think I would code do_it() or try_again( "...") or return;
as do_it() or return unless try_again("...");
You could also do return unless do_it() or try_again("...");
I'm vascillating between those as to which is the clearer?
Examine what is said, not who speaks.
The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.
| [reply] [d/l] [select] |
|
|
| [reply] [d/l] [select] |
|
|
!!$ok; is better, though I did say "In most cases". The only situation I can think of where
return $ok; instead of
return 1 if $ok; is going to fail, is if the calling code is using the boolean return value to do math?
Can you think of any others.
Examine what is said, not who speaks.
The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.
| [reply] [d/l] [select] |
|
|
|
|
Re: Double your return!!!!
by schweini (Friar) on Feb 19, 2003 at 06:16 UTC
|
Actually, i was looking for the same thing a while ago (didn't find anything)
i'm a bit surprised that everybody's soooo against thing thing. i, for one, have a Tk script littered with
sub bar
{
$foo = askFor( title => 'Name:' );
return undef if not defined $foo;
$baz = askFor( title => 'Cigarettes:' );
return undef if not defined $baz;
}
stuff. &askFor simpy pops up an entry form that offers a 'cancel', which MUST cancel the caller (&bar), so for me a double-return would be great. (a significant portion of my code is starting to be this dumb "return if .." thing
but i always remember that i started of with BASIC, and am therefore not able to grasp the horrendous evilness of this (someone once pointed out that a double return would be a GOTOism (which would be bad)).
could someone explain why this would lead to spaghetti code, and other nasty stuff?
(i'm really afraid that i'm doing things now that'll make me loose my hair in a week)
| [reply] [d/l] |
|
|
Boolean logic is your friend.
return undef unless
defined($name = askFor(title => 'Name:'))
and defined($cigs = askFor(title => 'Cigarettes:'));
That return undef should probably be a plain return, unless Tk is as fastidious about return values as GtkPerl.
Makeshifts last the longest. | [reply] [d/l] |
|
|
okay, i don't consider my self very 'leet, but - just to defend my honor (my parents could read this someday! :-), i DO know and.
that one doesn't really help a lot, because then my whole script would consist of a gigantic concatenation of and clauses.
besides - mea maxima culpa - i actually do stuff in between my user interaction things, so that one wouldn't work (should've showed that, though)
thanks for the tip, though!
by the way: what is the voting etiquette for situations like this? if i were an even bloodier newbie than i am, this one would have helped, and it's really nice of you to help me this way - OTOH, it didn't really help ME, and is a rather...ermm..."banal" thing (really no offense!).
i'll ++ it, but i was just wondering how "valuable" votes are....
kali nixta kai evxaristo
(pardon the horrific ASCII-sation :-)
| [reply] [d/l] [select] |
|
|
|
|
|
|
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';
| [reply] [d/l] [select] |
|
|
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.
| [reply] |
|
|
Re: Double your return!!!!
by parv (Parson) on Feb 19, 2003 at 04:11 UTC
|
I feel the need to have, what OP described above, when i
find myself explicitly returning some sort of true/false
value just to check whether the calling sub should continue.
An intersting idea indeed.
| [reply] |
Re: Double your return!!!!
by bsb (Priest) on Apr 08, 2003 at 23:52 UTC
|
sub return_if_true ($value) {
if $value {
leave where => caller(1), value => $value;
}
}
# and then just do:
return_if_true(result_of_big_long_calculation(...));
I think the leave where => caller(1) is the
double return I was curious about.
(p6language thread) | [reply] [d/l] |