Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: $foo->bar = 14;

by dws (Chancellor)
on Dec 29, 2002 at 21:40 UTC ( [id://222963]=note: print w/replies, xml ) Need Help??


in reply to $foo->bar = 14;

Before I'm going to change my coding style, I'd like to know what the community thinks of it.

One problem with the approach you set forth is that it doesn't survive subclassing.

Personally, I don't favor automatic creation of accessors (or mutators) at runtime. Development-time code generation can be good, but my experiences with delaying generation until runtime have almost always led to a debugging quagmire. I want accessors/mutators reflected directly in the source.

Replies are listed 'Best First'.
Re: Re: $foo->bar = 14;
by shotgunefx (Parson) on Dec 29, 2002 at 21:58 UTC
    One thing I like it for is CGI.
    I would rather say $cgi->param('foo') = 'bar' than $cgi->param(-name=>'foo',-value=>'bar')


    -Lee

    "To be civilized is to deny one's nature."
Re: Re: $foo->bar = 14;
by Juerd (Abbot) on Dec 29, 2002 at 23:36 UTC

    Personally, I don't favor automatic creation of accessors (or mutators) at runtime.

    I have this creation wrapped in BEGIN blocks, and have always been able to subclass them. Not that it matters much, because a module is run completely when it is used (how else can its return value be evaluated?). Inheritance itself is a run-time thing, so I can't see how it can break. Note that I'm not using AUTOLOAD here.

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.
    

Re^2: $foo->bar = 14;
by adrianh (Chancellor) on Dec 30, 2002 at 09:31 UTC
    One problem with the approach you set forth is that it doesn't survive subclassing.

    Eh?

    use strict; use warnings; { package Foo; sub new { bless {}, shift }; BEGIN { no strict 'refs'; for my $property (qw/foo bar/) { *$property = sub : lvalue { $_[0]->{$property} }; } } } { package Bar; use base qw(Foo); BEGIN { no strict 'refs'; for my $property (qw/fribble ni/) { *$property = sub : lvalue { $_[0]->{$property} }; } } }; my $o = Bar->new; $o->foo=1; $o->bar=2; $o->fribble=3; $o->ni=4; print join(', ', $o->foo, $o->bar, $o->fribble, $o->ni), "\n"; # produces # 1, 2, 3, 4

    or am I missing something?

      or am I missing something?

      No, I missed something. I was reacting based on past misadventures with something very similar, without having read your code carefully enough. What you've written will indeed survive subclassing, though debugging through those generated methods is still going to be a problem. Mea culpa.

        I'm not the author of the code :-)

        Personally I very rarely generate accessors - not because I dislike generating them, but because I so rarely have a situation where bare-access to the object attributes is appropriate.

        Since many perl coders seem to spend a lot of time worrying about generating accessors I guess I must be weird!

        That said, the times I have generated accessors I've not come across debugging issues. I'm curious what problems you've encountered - can you illustrate?

Log In?
Username:
Password:

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

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

    No recent polls found