Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Why breaking can() is acceptable

by hardburn (Abbot)
on Apr 06, 2004 at 13:57 UTC ( #342922=note: print w/ replies, xml ) Need Help??


in reply to Why breaking can() is acceptable

I'd prefer that people stop using AUTOLOAD when they don't need to. Specifically:

sub AUTOLOAD { my $self = shift; $self->{$AUTOLOAD} = shift if @_; $self->{$AUTOLOAD}; }

The actual code could be more complex, such as checking $AUTOLOAD against a list of allowed attributes, but the idea of runtime handling of accessors/mutators is the same. If you must have autogenerated accessors/mutators, please use symbol table manipulation instead if at all possible:

# Could be wrapped in a BEGIN block if you wanted to foreach my $field (qw/ foo bar baz /) { *$field = sub { my $self = shift; $self->{$field} = shift if @_; $self->{$field}; }; }

This is faster, is as memory-efficient as things get around Perl, and (IIRC) doesn't break can. Overall, it's even better to avoid spreading accessors/mutators all over your class design in the first place, but that's a seperate issue.

I understand the above isn't the only use of AUTOLOAD, but it's by far the most common. So if you're doing that, please stop and look for alternatives. Or better, fix your class design.

----
: () { :|:& };:

Note: All code is untested, unless otherwise stated


Comment on Re: Why breaking can() is acceptable
Select or Download Code
Re: Re: Why breaking can() is acceptable
by dragonchild (Archbishop) on Apr 06, 2004 at 14:18 UTC
    Ok. I agree with you, for that instance. However, how do you handle syntactic-sugar methods that are instance-specific?

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

      how do you handle syntactic-sugar methods that are instance-specific?

      That is something that probably comes down to a case-by-case basis, so I don't want to give any hard rules on it. What I do want is to stop people from using AUTOLOAD to handle accessors/mutators when other solutions are available. This seems to be the most common use of AUTOLOAD, and it needs to stop. (Please direct flames twards tchrist :)

      ----
      : () { :|:& };:

      Note: All code is untested, unless otherwise stated

        What about soft rules? :-)

        ------
        We are the carpenters and bricklayers of the Information Age.

        Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

Re: Re: Why breaking can() is acceptable
by tilly (Archbishop) on Apr 06, 2004 at 15:03 UTC
    And that is exactly what I was referring to with my comments on "typeglobbing closures instead of AUTOLOAD".

    I also usually don't wrap it in a BEGIN block. In the context of a module, there is no real reason to wrap it in a BEGIN block, because the module itself is wrapped in an implicit BEGIN block. Otherwise I prefer to not try to specify BEGIN ordering unless absolutely needed, instead specify ordering of events as simple as I can arrange. (But that was an old discussion with tye.)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (14)
As of 2014-09-19 19:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (144 votes), past polls