Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^4: "Fields" for "Objects"

by stvn (Monsignor)
on Jun 12, 2009 at 00:39 UTC ( [id://770797]=note: print w/replies, xml ) Need Help??


in reply to Re^3: "Fields" for "Objects"
in thread "Fields" for "Objects"

Each subsequent call to the method accesses the sub normally, i.e. through typeglob lookup.

Yes, this is true, however this is going deeper down the slippery slope where the amount of complex code to maintain is far outweighing the benefits of saving some typing. Take for instance, stack traces. Because you assigned an anon sub ref into a typeglob any stack traces will see the name of this method (and any other ones you created with AUTOLOAD) as simple __ANON__, which means you have no way of distinguishing them from one another. While this might not seem like much of an issue to some, it is a small details that would really be helpful when your up against the wall trying to fix a bug in the 11th hour.

Of course, this too is solveable, Ovid had a hack where he assigned a name to the __ANON__ typeglob slot using local I don't recall the details, and there is also Sub::Name on the CPAN. But either way your now depending on an XS module (Sub::Name) or doing some weird and nasty local/typeglob hackery.

That plumbing amounts to writing a proper import() subroutine which calls AUTOLOAD for the inherited fields and exports them to the inheriting class

Yes, but it wouldn't be that simple if you add in multiple inheritance and the possibility of conflicts/diamond inheritance. Also I am not sure how this would work for deeply inherited fields (more then one level away), but perhaps I am just not understanding the solution fully. Again, all might be solvable problems, but now you've gone from "saving myself some tedious typing" to "building my own object system with AUTOLOAD".

But then, you're right, why reinvent the wheel (there are reasons, though) if there are plenty modules out there which handle that.

The single best reason to re-invent the wheel is to do it as a learning tool. I highly recommend the practice, especially if you have other well written versions to learn/steal from. Moose would not exist if it were not for CLOS and more specifically the Art of the MetaObject Protocol book.

-stvn

Replies are listed 'Best First'.
Re^5: "Fields" for "Objects"
by shmem (Chancellor) on Jun 12, 2009 at 01:07 UTC
    ... or doing some weird and nasty local/typeglob hackery.

    Keep in mind that talking about Perl5, the specification is the implementation. So that "weird and nasty hackery" is, by flipping the enlightenment bit, Higher Order Perl... ;)

    I'd say - dealing with Perl5, expect dragons. But they are teaching dragons. We even do have one here...

    update:

    Of course, this too is solveable, Ovid had a hack where he assigned a name to the __ANON__ typeglob slot using local I don't recall the details, and there is also Sub::Name on the CPAN. But either way your now depending on an XS module (Sub::Name) or doing some weird and nasty local/typeglob hackery.

    Nah... look, ma, no XS, no __ANON__:

    sub AUTOLOAD { my $self = shift; my $type = ref($self) or croak "$self is not an object"; my $name = $AUTOLOAD; $name =~ s/.*://; # strip fully-qualified portion unless (exists $self->{_permitted}->{$name} ) { croak "Can't access `$name' field in class $type"; } eval ("sub $AUTOLOAD { my (\$self,\$value) = \@_; \$self->{$name} = \$value if defined \$value; \$self->{$name}; }"); die $@ if $@; goto &$AUTOLOAD; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-23 11:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found