Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Determination of chained method call context

by kyle (Abbot)
on Mar 31, 2009 at 16:07 UTC ( #754452=note: print w/ replies, xml ) Need Help??


in reply to Determination of chained method call context

This isn't a definite solution, but I guess it could help sometimes.

package Bloodnok; sub new { bless {}, shift } sub call { my ( $self, $position ) = @_; my $wa = wantarray; if ( $wa ) { print "$position: list\n"; } elsif ( defined $wa ) { print "$position: scalar\n"; } else { print "$position: void\n"; } return $self; } package main; my $yo_sup_b = Bloodnok->new(); $yo_sup_b->call( 'void start' ) ->call( 'void mid' ) ->call( 'void end' ); scalar $yo_sup_b->call( 'scalar start' ) ->call( 'scalar mid' ) ->call( 'scalar end' ); () = $yo_sup_b->call( 'list start' ) ->call( 'list mid' ) ->call( 'list end' ); __END__ void start: scalar void mid: scalar void end: void scalar start: scalar scalar mid: scalar scalar end: scalar list start: scalar list mid: scalar list end: list

Long story short, wantarray will always show a scalar context for calls before the end of a chain, but it shows the original context at the end of the chain. If your chain is in a scalar context anyway, that doesn't help at all, but in the other two contexts you can tell you must be at the end.


Comment on Re: Determination of chained method call context
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://754452]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2014-10-02 05:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (49 votes), past polls