Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://754452]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2018-06-24 05:39 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.