On Oct 23, 2:37 pm, da...@davidfavor.com (David Favor) wrote: [omitted]
> > Just be sure you know what you are doing. Adding a method to somone else's > > class can be considered rude. See the NOTE in perldoc perlmodlib. > > sub add_class_method { > my($class,$name,$code) = @_; > no strict 'refs'; > ${$class . '::'}{$name} = $code; > > } > > sub somewhere_in_da_code { > > ... ... ... > > my $class = 'Qpsmtpd::Transaction'; > my $name = 'test_method'; > my $code = \&test_method; > > add_class_method($class,$name,$code); > > # all three of these invocations works correctly > test_method($self); > &$code($self); > $self->test_method; ^^^^^^^^^^^^^^^^^^^^^^^ Hm, beware however that 5.12.1 (Strawberry perl) fails on the last invocation: .... no strict 'refs'; # ${$class . '::'}{$name} = $code; # fails *{"${class}::$name"} = $code; # succeeds } Here's the error I saw: Cannot convert a reference to CODE to typeglob at ... 'use diagnostics' explanation: (F) You manipulated Perl's symbol table directly, stored a reference in it, then tried to access that symbol via conventional Perl syntax. The access triggers Perl to autovivify that typeglob, but it there is no legal conversion from that type of reference to a typeglob. The direct typeglob assignment works but P.Johnson's solution is preferable to avoid the symbolic ref. -- Charles DeRykus -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/