Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Camel book: why invoke in the manner @{[&whowasi]}?

Reply
Thread Tools

Camel book: why invoke in the manner @{[&whowasi]}?

 
 
Suresh Govindachar
Guest
Posts: n/a
 
      08-11-2004
#!/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


 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-11-2004
Suresh Govindachar wrote:
> 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


It gets interpolated in a doublequoted context.

> Please explain what the first method of invocation is all about.


sub foo { "Now foo() was interpolated!" }
print "foo()\n";
print "@{ [foo()] }\n";

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
Alan Stewart
Guest
Posts: n/a
 
      08-12-2004
On Wed, 11 Aug 2004 22:01:57 GMT, "Suresh Govindachar"
<(E-Mail Removed)> wrote:

>#!/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]]";


Interpolated into a string.

>
> print "\n", 'carp "@{[&whowasi]}: ";', "\n";
> carp "@{[&whowasi]}: ";


Interpolated.

>
> print "\n", 'carp &whowasi if $DEBUG;' ,"\n";
> carp &whowasi if $DEBUG;


Not interpolated.

>
> debug();
> print "\nafter setting DEBUG:\n";
> carp &whowasi if $DEBUG;


Not interpolated.

>
> 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__


Variables will interpolate into strings but functions will not. This
is a well known trick to get a function into a quoted string.
Functions will execute to get the value of an item in a list, so
[$whowasi] produces an anonymous list reference which is de-referenced
by @{ } to an array that will interpolate.

Back up a page to Camel pg 377 for the other way !

Alan

 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      08-12-2004

Suresh Govindachar wrote:

> 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


Function calls do not interpolate into double-quoted strings so if you
want to put the result of a function call into the middle a string you
can either use explicit concatenation or use the @{[...]} trick.

See FAQ: How do I expand function calls in a string?

If you ever see the @{[ somefunc ]} construct not in an interpolative
context then it may be being used for it's other effect of 'uncoupling'
values in the situation where somefunc returns lvalues.

Calling the function call syntax with a leading & and without any ()
supresses all the @_ handling. The @_ seen inside the whowasi function
is the caller's @_. (See perlsub). This is occasionaly really useful
in advanced programming but more often is just done as a (possibly
premature) optomisation or through ignorance.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
GSM and GPRS,CAMEL, INAP ,camel tutorials Ahsan UK VOIP 0 09-18-2006 07:59 AM
DVD Verdict reviews: THE STORY OF THE WEEPING CAMEL and more! DVD Verdict DVD Video 0 04-18-2005 08:12 AM
Why Camel is Better than Perldoc Sara Perl Misc 6 11-12-2003 10:03 PM



Advertisments