Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: multiple method calls against the same object, revisited

by ihb (Deacon)
on Dec 28, 2004 at 02:44 UTC ( #417686=note: print w/ replies, xml ) Need Help??


in reply to Re: multiple method calls against the same object, revisited
in thread multiple method calls against the same object, revisited

What happens when you want to have a method without any arguments in the middle of those other methods? This problem is the reason Aristotle used array references, I think.

A possible solution would be to "escape" the methods without arguments, yielding

my $window = Gtk2::Window->new( "toplevel" )->call_method_list( signal_connect => [ delete_event => sub { Gtk2->main_quit } ], set_title => "Test", \'my_argumentless_method', # <--------------- set_border_width => 15, add => Gtk2::Button->new( "Quit" )->call_method_list( signal_connect => [ clicked => sub { Gtk2->main_quit } ] ), \'show_all', );
and change the implementation to (untested)
use Carp qw( croak ); sub UNIVERSAL::call_method_list { my $target = shift; while ( scalar @_ ) { my $method = shift @_; my @args = (ref $method) ? () : (ref($_[0]) eq 'ARRAY') ? @{shift} + : shift; $method = $$method if ref $method; eval { $target->$method( @args ) }; if( $@ ) { croak( $@ ) } } return $target; }

ihb

Update: Apparently I should've read the code closer. The last case of a single method with no following element threw me off.

See perltoc if you don't know which perldoc to read!
Read argumentation in its context!


Comment on Re^2: multiple method calls against the same object, revisited
Select or Download Code
Re^3: multiple method calls against the same object, revisited
by simonm (Vicar) on Dec 28, 2004 at 07:48 UTC
    What happens when you want to have a method without any arguments in the middle of those other methods?

    Just write it with an empty array reference:

    my $window = Gtk2::Window->new( "toplevel" )->call_method_list( signal_connect => [ delete_event => sub { Gtk2->main_quit } ], set_title => "Test", my_argumentless_method => [], set_border_width => 15, add => Gtk2::Button->new( "Quit" )->call_method_list( signal_connect => [ clicked => sub { Gtk2->main_quit } ] ), 'show_all', );

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2015-07-05 18:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (67 votes), past polls