On 12/3/06, Bill Jones <[EMAIL PROTECTED]> wrote:
On 11/24/06, JupiterHost.Net <[EMAIL PROTECTED]> wrote:
> The trick is I can't seem to goto() return in
> do_some_stuff_and_return(), I'm sure since its so deep down...
I guess I'm totally confused; I don't see how a goto can return back
to the sub-rotine that called it:
#! perl -w
use strict;
my $global = 99;
&main();
# The below goto returns here -- not back to the sub-routine.
print "Primary: Did I catch the new value $global?\n";
exit;
sub main() {
print "Main0: \$global is $global\n";
goto &the_goto if $global =~ /99/; # Goto does NOT return back here...
$global = 60;
print "Main1: Did I catch the new value $global?\n";
}
sub the_goto() {
print "Goto0: \$global is $global\n";
$global = 0;
}
__END__
The use of goto in this content is like using AUTOLOAD which causes
the called routine to not return back to where it was called from but
actually continue at the point AFTER the previous routine -- a little
confusing to us beginners.
This is the behavior of AUTOLOAD, which uses goto, but that's not
quite how it works. 'goto &sub' replaces the current routine. It's
like exec() for subroutines. Consider the following:
#!/usr/bin/perl
use warnings;
use strict;
our $global = 99;
my $lexical = 99;
my $count = 0;
&mysub(25,75);
$count++;
the_goto();
print "main0.0: global value is $global\n";
print "main0.0: lexical value is $lexical\n";
sub mysub() {
local $global = shift;
my $lexical = $global;
print "mysub0.0: \$global is $global\n";
print "mysub0.0: \$lexical is $lexical\n";
the_goto();
$count++;
goto &the_goto;
$global = 60;
print "mysub0.1: Did I catch the new value $global?\n";
}
sub the_goto() {
print "goto$count.0: \$global is $global\n";
print "goto$count.0: \$lexical is $lexical\n";
local $global = shift;
my $lexical = $global;
print "goto$count.1: \$global is $global\n"; #warning w/out goto
print "goto$count.1: \$lexical is $lexical\n"; #warning w/out goto
}
__END__
Notice that we never pass the_goto() anything for @_. with goto,
the_goto() completely replaces mysub(), inheriting its @_ (but not its
local variables). This is different than simply calling it from within
a subroutine or returning the caller early and then executing the
goto.
HTH,
-- jay
--------------------------------------------------
This email and attachment(s): [ ] blogable; [ x ] ask first; [ ]
private and confidential
daggerquill [at] gmail [dot] com
http://www.tuaw.com http://www.downloadsquad.com http://www.engatiki.org
values of β will give rise to dom!