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

Re^9: How can I call a Perl sub with an object from a sub name stored in a var?

by tobyink (Canon)
on Dec 10, 2020 at 12:05 UTC ( #11124953=note: print w/replies, xml ) Need Help??


in reply to Re^8: How can I call a Perl sub with an object from a sub name stored in a var?
in thread How can I call a Perl sub with an object from a sub name stored in a var?

I did try undef, but got warnings, so figured it was verboten. I ought to have continued with my experiments.

Here's something fun. Acme::Ref breaks on recent Perls, but it's a one line patch to fix it. (The _deref function needs to take a long argument instead of an int.)

use strict; use warnings; use feature 'say'; { package Local::Class; sub new { my ( $class ) = ( shift ); bless {}, $class; } sub AUTOLOAD { my ( $method ) = ( our $AUTOLOAD =~ /([^:]+)$/ ); if ( $method =~ /^(ARRAY|HASH|SCALAR|REF)/i ) { require Acme::Ref; my $real_coderef = $_[0]->can("HANDLE_$1") or die "Cannot handle $1 method"; splice @_, 1, 0, do { local $SIG{__WARN__} = sub {}; Acme: +:Ref::deref($method); }; goto $real_coderef; } return if $method eq 'DESTROY'; die "Could not load $method via AUTOLOAD"; } sub HANDLE_ARRAY { my ( $self, $array, @args ) = ( shift, shift, @_ ); say for @$array; } sub HANDLE_HASH { say "whatever"; } # etc... }; my $object = 'Local::Class'->new(); my $array = [ "Hello", "world" ]; $object->$array; $object->${\ [ "And", "again" ] };

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2021-10-26 10:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (90 votes). Check out past polls.

    Notices?