Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: AUTOLOAD - the good, the bad, and the ugly

by raptnor2 (Beadle)
on Oct 15, 2004 at 02:56 UTC ( [id://399383]=note: print w/replies, xml ) Need Help??


in reply to AUTOLOAD - the good, the bad, and the ugly

I've used it to wrap classes that I wouldn't know anything about, but needed to provide add-on methods for component management. Using this technique, I don't have to care what someone wants to use as a component (as long as it's a class).

A snippet from IOC::Lite follows:

sub AUTOLOAD{ my($self, @args) = @_; my @method_and_structure = split m|\:\:|, $AUTOLOAD; my $method = $method_and_structure[@method_and_structure-1]; if ($self->{object}->can($method)){ return $self->{object}->$method(@args); } die ">$method is not found on " . $self->get_type() . "\n"; }

I've taken the concept a little further, almost AOP like, in the version of IOC::Lite I'm working on now. That way I can add all types of features to objects the container doesn't own. The basic component management methods will be first, followed closely by dynamic logging. Not sure where I'll head from there.

Sorry if this is just rambling. Bottom line: I think it would be quite useful on large software projects. I can't tell you how many times I wished I had this feature when coding in (C/C++/Java/etc)

Cheers,

John

janitored by ybiC: Replace frowned-upon <pre> tags with Monastery-standard <code> tags

Replies are listed 'Best First'.
Re^2: AUTOLOAD - the good, the bad, and the ugly
by dragonchild (Archbishop) on Oct 15, 2004 at 04:23 UTC
    A nit.
    my $method = $method_and_structure[@method_and_structure-1];
    is better written (and slightly faster) as
    my $method = $method_and_structure[-1];

    But, most examples I've seen tend to do the one of the following:

    my $method = (split '::', $AUTOLOAD)[-1]; #### my ($method) = $AUTOLOAD =~ /([^:]+)$/; #### (my $method = $AUTOLOAD) =~ s/.*:://;

    In that last, you want to use .* as opposed to .*? because you want it to be as greedy as possible.

    Update: Fixed slice to scalar access.

    Update: Added parentheses in second-to-last example.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      I tend to do my ($method) = $AUTOLOAD =~ /.*::(.*)/s; which I find nice and clean.

      ihb

      See perltoc if you don't know which perldoc to read!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-19 20:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found