in reply to Re: Syntax question: Method call, where method name is calculated from an expression
in thread Syntax question: Method call, where method name is calculated from an expression
Looks great, but this doesn't work if the method expects parameters. For instance:
The reason is that can() returns only a reference to the method, but the object ($x) is lost on the way. A workaround would bepackage Obj; sub new { my $class=shift; my $self={ x => 'uh' }; bless $self, $class; } sub howl { my ($self,$arg)=@_; print($self->{x}," $arg\n"); } package main; sub mname { 'howl' } my $x=Obj->new(); $x->howl('oh'); $x->can(mname)->('ah');
but this is ugly, since it requires us to mention the object twice.$x->can(mname)->($x,'ah');
|
|---|