Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Can I please have *simple* modules?

by Perl Mouse (Chaplain)
on Nov 23, 2005 at 20:45 UTC ( #511238=note: print w/replies, xml ) Need Help??

in reply to Can I please have *simple* modules?

While I can understand the wish for simple modules in general, I fail to see why anyone would want to use a module to generate their accessors.

Accessors are simple. They're one liners. I find code that just spells out the accessors much simpler than code that uses a module to create the accessors for them - no matter how simple the module is.

# Look ma, no module! sub get_attribute {$attribute {refaddr $_[0]}} sub set_attribute {$attribute {refaddr $_[0]} = $_[1]}
Perl --((8:>*

Replies are listed 'Best First'.
Re^2: Can I please have *simple* modules?
by Ovid (Cardinal) on Nov 23, 2005 at 20:51 UTC

    Why on earth would you want to retype that over and over again for every accessor/mutator?


    New address of my CGI Course.

      Huh? Retype? What primitive editor do you have?
      Perl --((8:>*

        No, I don't have to retype them. I have a reasonable editor and it can chunk out plenty of boilerplate code for me. Regardless, code duplication is code duplication. If you want to put a bunch of cut-n-paste boilerplate in your modules, that's fine. I just don't like to do it in my modules and I don't want to have to maintain it in other's modules. That means more code I or a maintenance programmer has to wade through to understand what's going on.

        Additionally, your method assumes a particular implementation (in this case, a blessed hashref). Not only does it make it easy for another programmer to violate encapsulation (even if it's only by accident), it also means that it's more work to switch your object to a blessed array ref or inside out objects.

        There's been plenty written about why duplicating even simple things like accessors/mutators is bad. Rather than want to do that, I just do this:

        use Class::BuildMethods qw/foo bar baz/;

        With that, I get three accessors/mutators without code duplication or reliance on the internals. Further, it's easy to extend them with default values or validation code. In short, by my writing this module once, my other code is cleaner and more flexible.


        New address of my CGI Course.

Re^2: Can I please have *simple* modules?
by demerphq (Chancellor) on Nov 24, 2005 at 11:04 UTC

    So thats the accessor code you use. Cool. But wheres the DESTROY? And how do you manage it? Id expect some infrastructure to manage the attributes. Something like:

    package Foo; use Scalar::Util qw(refaddr); use strict; use warnings; BEGIN { my @destroy; sub DESTROY { my $addr= refaddr $_[0]; foreach my $hash (@destroy) { delete $hash->{$addr}; } } sub reg_attr { push @destroy,\my %hash; \%hash } } BEGIN { my $attribute=reg_attr(); sub get_attribute{ $attribute->{refaddr $_[0]} } sub set_attribute{ $attribute->{refaddr $_[0]} = $_[1] } }

Re^2: Can I please have *simple* modules?
by Aristotle (Chancellor) on Nov 24, 2005 at 14:55 UTC

    #11911 You wrote the same thing twice here. The cardinal rule of programming is that you never ever write the same thing twice.

    Makeshifts last the longest.

      What? You only have at most one for() statement, or one while (<$handle>) statement per program? Or do you factor that out as well?

      Never writing the same thing twice doesn't work on a low level. Accessors are low level.

      Perl --((8:>*

        They follow a repetitive pattern. So you write a closure that encapsulates this pattern and assign instances of it to globs in your symbol table. This is Perl, not C. Copypasting is stupid busywork.

        Makeshifts last the longest.

        Damn, you tell me that after I spent hours trying to figure out how to avoid writing and rewriting #!/usr/bin/perl in all my programs! ;-)

        (Well, actually I tend to use editor templates for that...)

Re^2: Can I please have *simple* modules?
by shotgunefx (Parson) on Nov 23, 2005 at 23:35 UTC
    I'll often just use a loop and a closure to make mine.


    perl digital dash (in progress)
Re^2: Can I please have *simple* modules?
by belg4mit (Prior) on Dec 01, 2005 at 18:40 UTC
    What about AUTOLOAD? Infinitely extendable, allows the user to add attributes without sub-classing :-D

    In Bob We Trust, All Others Bring Data.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://511238]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2018-05-20 14:37 GMT
Find Nodes?
    Voting Booth?