S
Suresh Govindachar
#!/use/bin/perl
BEGIN {(*STDERR = *STDOUT) || die;}
use diagnostics;
use warnings;
use strict;
=head
Hello,
In the 'Tying Hashes' section of the Camel book (pg 378+),
a subroutine called whowasi is invoked in the
following two ways,
1) @{[&whowasi]}
and 2) &whowasi
I could not figure out what the first method of
invocation does and so wrote this code to see
the difference in results. But I find no
difference in the results of the two invocations!
(The output is given below after the __END__.)
Please explain what the first method of invocation
is all about.
Thanks,
--Suresh
=cut
use Carp;
sub whowasi { (caller(1))[3] . "()" }
my $DEBUG = 0;
sub debug { $DEBUG = @_ ? shift : 1 }
sub the_place
{
print "\n",'carp "usage: @{[ &whowasi ]} [USER [DOTDIR]]";',"\n";
carp "usage: @{[ &whowasi ]} [USER [DOTDIR]]";
print "\n", 'carp "@{[&whowasi]}: ";', "\n";
carp "@{[&whowasi]}: ";
print "\n", 'carp &whowasi if $DEBUG;' ,"\n";
carp &whowasi if $DEBUG;
debug();
print "\nafter setting DEBUG:\n";
carp &whowasi if $DEBUG;
print "\n",'croak "usage: @{[ &whowasi ]} [USER [DOTDIR]]";',"\n";
croak "usage: @{[ &whowasi ]} [USER [DOTDIR]]";
print "\nshould NOT see this\n";
}
sub the_caller
{
the_place(53);
}
sub the_sub
{
the_caller();
}
print "in main:\n";
the_sub();
__END__
Output is as follows:
in main:
carp "usage: @{[ &whowasi ]} [USER [DOTDIR]]";
usage: main::the_place() [USER [DOTDIR]] at ..\tms\drafts\current line 40
main::the_place(53) called at ..\tms\drafts\current line 59
main::the_caller() called at ..\tms\drafts\current line 64
main::the_sub() called at ..\tms\drafts\current line 68
carp "@{[&whowasi]}: ";
main::the_place(): at ..\tms\drafts\current line 43
main::the_place(53) called at ..\tms\drafts\current line 59
main::the_caller() called at ..\tms\drafts\current line 64
main::the_sub() called at ..\tms\drafts\current line 68
carp &whowasi if $DEBUG;
after setting DEBUG:
main::the_place() at ..\tms\drafts\current line 50
main::the_place(53) called at ..\tms\drafts\current line 59
main::the_caller() called at ..\tms\drafts\current line 64
main::the_sub() called at ..\tms\drafts\current line 68
croak "usage: @{[ &whowasi ]} [USER [DOTDIR]]";
Uncaught exception from user code:
usage: main::the_place() [USER [DOTDIR]] at ..\tms\drafts\current line 53
main::the_place(53) called at ..\tms\drafts\current line 59
main::the_caller() called at ..\tms\drafts\current line 64
main::the_sub() called at ..\tms\drafts\current line 68
main::the_place(53) called at ..\tms\drafts\current line 59
main::the_caller() called at ..\tms\drafts\current line 64
main::the_sub() called at ..\tms\drafts\current line 68
BEGIN {(*STDERR = *STDOUT) || die;}
use diagnostics;
use warnings;
use strict;
=head
Hello,
In the 'Tying Hashes' section of the Camel book (pg 378+),
a subroutine called whowasi is invoked in the
following two ways,
1) @{[&whowasi]}
and 2) &whowasi
I could not figure out what the first method of
invocation does and so wrote this code to see
the difference in results. But I find no
difference in the results of the two invocations!
(The output is given below after the __END__.)
Please explain what the first method of invocation
is all about.
Thanks,
--Suresh
=cut
use Carp;
sub whowasi { (caller(1))[3] . "()" }
my $DEBUG = 0;
sub debug { $DEBUG = @_ ? shift : 1 }
sub the_place
{
print "\n",'carp "usage: @{[ &whowasi ]} [USER [DOTDIR]]";',"\n";
carp "usage: @{[ &whowasi ]} [USER [DOTDIR]]";
print "\n", 'carp "@{[&whowasi]}: ";', "\n";
carp "@{[&whowasi]}: ";
print "\n", 'carp &whowasi if $DEBUG;' ,"\n";
carp &whowasi if $DEBUG;
debug();
print "\nafter setting DEBUG:\n";
carp &whowasi if $DEBUG;
print "\n",'croak "usage: @{[ &whowasi ]} [USER [DOTDIR]]";',"\n";
croak "usage: @{[ &whowasi ]} [USER [DOTDIR]]";
print "\nshould NOT see this\n";
}
sub the_caller
{
the_place(53);
}
sub the_sub
{
the_caller();
}
print "in main:\n";
the_sub();
__END__
Output is as follows:
in main:
carp "usage: @{[ &whowasi ]} [USER [DOTDIR]]";
usage: main::the_place() [USER [DOTDIR]] at ..\tms\drafts\current line 40
main::the_place(53) called at ..\tms\drafts\current line 59
main::the_caller() called at ..\tms\drafts\current line 64
main::the_sub() called at ..\tms\drafts\current line 68
carp "@{[&whowasi]}: ";
main::the_place(): at ..\tms\drafts\current line 43
main::the_place(53) called at ..\tms\drafts\current line 59
main::the_caller() called at ..\tms\drafts\current line 64
main::the_sub() called at ..\tms\drafts\current line 68
carp &whowasi if $DEBUG;
after setting DEBUG:
main::the_place() at ..\tms\drafts\current line 50
main::the_place(53) called at ..\tms\drafts\current line 59
main::the_caller() called at ..\tms\drafts\current line 64
main::the_sub() called at ..\tms\drafts\current line 68
croak "usage: @{[ &whowasi ]} [USER [DOTDIR]]";
Uncaught exception from user code:
usage: main::the_place() [USER [DOTDIR]] at ..\tms\drafts\current line 53
main::the_place(53) called at ..\tms\drafts\current line 59
main::the_caller() called at ..\tms\drafts\current line 64
main::the_sub() called at ..\tms\drafts\current line 68
main::the_place(53) called at ..\tms\drafts\current line 59
main::the_caller() called at ..\tms\drafts\current line 64
main::the_sub() called at ..\tms\drafts\current line 68