in reply to Re: A useful use of goto
in thread A useful use of goto

I thought so too, but at least this sample program fails for me:

#!perl -w use strict; use Data::Dumper; $Data::Dumper::Sortkeys = 1; my %handler = ( hello => sub() { print "hello world\n" }, ); my ($ok,$err); my @handlers = keys %handler; my $cfg_str = 'test(); hello();'; { no strict 'refs'; # We don't want to introduce another scope, as that will # negate the effect of the local: warn Dumper \%{main::}; # A first my own declaration local *{test} = sub { print "This is a test\n"; }; local *{$_} = $handler{ $_ } for keys %handler; warn Dumper \%{main::}; print '$handler{hello} ', $handler{ hello },"\n"; print '*::{hello} [',*::{ hello } || '<none>',"]\n"; $ok = eval $cfg_str; $err = $@; } warn $err if $err; __END__ Name "main::test" used only once: possible typo at tmp.pl line 22. $VAR1 = { ' => *{':'}, '&#9788;' => *{'::&#9788;'}, '&#8597;' => *{'::&#8597;'}, '&#8616;ARNING_BITS' => *{'::&#8616;ARNING_BITS'}, '&#8593;' => *{'::&#8593;'}, '!' => *{'::!'}, '"' => *{'::"'}, '$' => *{'::$'}, '+' => *{'::+'}, '-' => *{'::-'}, '/' => *{'::/'}, '0' => *{'::0'}, '1' => *{'::1'}, '<none>::' => *{'::<none>::'}, '@' => *{'::@'}, 'ARGV' => *::ARGV, 'ActivePerl::' => *{'::ActivePerl::'}, 'B::' => *{'::B::'}, 'BEGIN' => *::BEGIN, 'CORE::' => *{'::CORE::'}, 'Carp::' => *{'::Carp::'}, 'Config::' => *{'::Config::'}, 'DATA' => *::DATA, 'DB::' => *{'::DB::'}, 'Data::' => *{'::Data::'}, 'Dumper' => *::Dumper, 'DynaLoader::' => *{'::DynaLoader::'}, 'ENV' => *::ENV, 'Exporter::' => *{'::Exporter::'}, 'INC' => *::INC, 'IO::' => *{'::IO::'}, 'Internals::' => *{'::Internals::'}, 'PerlIO::' => *{'::PerlIO::'}, 'Regexp::' => *{'::Regexp::'}, 'SIG' => *::SIG, 'STDERR' => *::STDERR, 'STDIN' => *::STDIN, 'STDOUT' => *::STDOUT, 'Scalar::' => *{'::Scalar::'}, 'UNIVERSAL::' => *{'::UNIVERSAL::'}, 'Win32::' => *{'::Win32::'}, 'XSLoader::' => *{'::XSLoader::'}, '_' => *::_, '_<..\\activeperl.c' => *{'::_<..\\activeperl.c'}, '_<..\\perlio.c' => *{'::_<..\\perlio.c'}, '_<..\\universal.c' => *{'::_<..\\universal.c'}, '_<..\\xsutils.c' => *{'::_<..\\xsutils.c'}, '_<.\\win32.c' => *{'::_<.\\win32.c'}, '_<C:/Program Files/Perl/lib/auto/Data/Dumper/Dumper.dll' => + *{'::_<C:/Program Files/Perl/lib/auto/Data/Dumper/Dumper.dll' }, '_<Dumper.c' => *{'::_<Dumper.c'}, '_<dl_win32.c' => *{'::_<dl_win32.c'}, '_<perllib.c' => *{'::_<perllib.c'}, '__ANON__' => *::__ANON__, 'attributes::' => *{'::attributes::'}, 'bytes::' => *{'::bytes::'}, 'main::' => *{'::main::'}, 'overload::' => *{'::overload::'}, 'stderr' => *::stderr, 'stdin' => *::stdin, 'stdout' => *::stdout, 'strict::' => *{'::strict::'}, 'test' => *::test, 'utf8::' => *{'::utf8::'}, 'warnings::' => *{'::warnings::'} }; $VAR1 = { ' => *{':'}, '&#9788;' => *{'::&#9788;'}, '&#8597;' => *{'::&#8597;'}, '&#8616;ARNING_BITS' => *{'::&#8616;ARNING_BITS'}, '&#8593;' => *{'::&#8593;'}, '!' => *{'::!'}, '"' => *{'::"'}, '$' => *{'::$'}, '+' => *{'::+'}, '-' => *{'::-'}, '/' => *{'::/'}, '0' => *{'::0'}, '1' => *{'::1'}, '<none>::' => *{'::<none>::'}, '@' => *{'::@'}, 'ARGV' => *::ARGV, 'ActivePerl::' => *{'::ActivePerl::'}, 'B::' => *{'::B::'}, 'BEGIN' => *::BEGIN, 'CORE::' => *{'::CORE::'}, 'Carp::' => *{'::Carp::'}, 'Config::' => *{'::Config::'}, 'DATA' => *::DATA, 'DB::' => *{'::DB::'}, 'Data::' => *{'::Data::'}, 'Dumper' => *::Dumper, 'DynaLoader::' => *{'::DynaLoader::'}, 'ENV' => *::ENV, 'Exporter::' => *{'::Exporter::'}, 'INC' => *::INC, 'IO::' => *{'::IO::'}, 'Internals::' => *{'::Internals::'}, 'PerlIO::' => *{'::PerlIO::'}, 'Regexp::' => *{'::Regexp::'}, 'SIG' => *::SIG, 'STDERR' => *::STDERR, 'STDIN' => *::STDIN, 'STDOUT' => *::STDOUT, 'Scalar::' => *{'::Scalar::'}, 'UNIVERSAL::' => *{'::UNIVERSAL::'}, 'Win32::' => *{'::Win32::'}, 'XSLoader::' => *{'::XSLoader::'}, '_' => *::_, '_<..\\activeperl.c' => *{'::_<..\\activeperl.c'}, '_<..\\perlio.c' => *{'::_<..\\perlio.c'}, '_<..\\universal.c' => *{'::_<..\\universal.c'}, '_<..\\xsutils.c' => *{'::_<..\\xsutils.c'}, '_<.\\win32.c' => *{'::_<.\\win32.c'}, '_<C:/Program Files/Perl/lib/auto/Data/Dumper/Dumper.dll' => + *{'::_<C:/Program Files/Perl/lib/auto/Data/Dumper/Dumper.dll' }, '_<Dumper.c' => *{'::_<Dumper.c'}, '_<dl_win32.c' => *{'::_<dl_win32.c'}, '_<perllib.c' => *{'::_<perllib.c'}, '__ANON__' => *::__ANON__, 'attributes::' => *{'::attributes::'}, 'bytes::' => *{'::bytes::'}, 'hello' => *::hello, 'main::' => *{'::main::'}, 'overload::' => *{'::overload::'}, 'sort::' => *{'::sort::'}, 'stderr' => *::stderr, 'stdin' => *::stdin, 'stdout' => *::stdout, 'strict::' => *{'::strict::'}, 'test' => *::test, 'utf8::' => *{'::utf8::'}, 'warnings::' => *{'::warnings::'} }; $handler{hello} CODE(0x212e788) *::{hello} [<none>] This is a test Undefined subroutine &main::hello called at (eval 1) line 1.

... and it fails in a most weird way, as *::hello gets a slot after the loop, but it can't find hello() while it can find test().

Replies are listed 'Best First'.
Re^3: A useful use of goto
by JavaFan (Canon) on Apr 12, 2010 at 10:00 UTC
    It seems that while the if modifier doesn't introduce a scope, the for modifier does (probably to localize $_). So, my suggestion isn't going to work. :-(.