Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: "Fields" for "Objects"

by stvn (Monsignor)
on Jun 10, 2009 at 19:45 UTC ( [id://770433]=note: print w/replies, xml ) Need Help??


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

Honestly, use of AUTOLOAD for this kind of stuff is just really a bad idea. You will outgrow this solution all too quickly, not to mention the fact that your code is broken once you introduce inheritance since you have no mechanism to inherit the fields. Sure that problem is solve-able but not without a bunch of tedious plumbing as well. It should also be noted that AUTOLOAD introduces something like a 400% performance penalty over normal method calls (which only gets bigger the more complex your AUTOLOAD gets).

-stvn

Replies are listed 'Best First'.
Re^3: "Fields" for "Objects"
by shmem (Chancellor) on Jun 11, 2009 at 17:41 UTC
    It should also be noted that AUTOLOAD introduces something like a 400% performance penalty over normal method calls

    That penalty can be avoided for every but the first call to the autoloaded setter/getter function, if the AUTOLOAD block generates a subroutine living in a typeglob, just as AutoLoader does:

    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"; } my $sub = sub { my ($self,$value) = @_; $self->{$name} = $value if defined $value; $self->{$name}; }; { no strict 'refs'; *{$name} = $sub; } goto $sub; }

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

    not to mention the fact that your code is broken once you introduce inheritance since you have no mechanism to inherit the fields. Sure that problem is solve-able but not without a bunch of tedious plumbing as well.

    That plumbing amounts to writing a proper import() subroutine which calls AUTOLOAD for the inherited fields and exports them to the inheriting class. But then, you're right, why reinvent the wheel (there are reasons, though) if there are plenty modules out there which handle that. There's Moose, classes, Class::Accessor, ...

      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
        ... 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; }
Re^3: "Fields" for "Objects"
by dekimsey (Sexton) on Jun 11, 2009 at 02:26 UTC
    The only good reason to use AUTOLOAD is when you already know you need to use it. It has a magic-from-afar type behavior that can be weird.
    Danny.

Log In?
Username:
Password:

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

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

    No recent polls found