Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Unfortunately, Object::Tiny accessors are read-only.

    So there's one little thing that Class::Accessor has over Object::Tiny. (For about 30 seconds until Adam goes and adds it.)

    Otherwise, Object::Tiny++

    • Accessors ARE read-only...

      Frankly, I don't get this obsession people have with mutators.

      90% of the time, you want to make a data object of some sort with the attributes fixed at create-time.

      90% of the time, it makes little to no sense to have values changing after the object is created.

      This sort of thing is silly.

      my $object = Foo->new;
      $object->param1('foo');
      $object->param2('bar');


      It leaves the code in a transitional state that may will be illegal.

      FAR better to just provide it to the constructor, en
      • 90% of the time, it makes little to no sense to have values changing after the object is created.

        This sort of thing is silly.

        my $object = Foo->new;
        $object->param1('foo');
        $object->param2('bar');

        If you would be so kind to add write accessors to Object::Tiny you could improve even your own code once you come to those last 10%, e.g. Module::CGI::Install line 130:

        $self->{cgi_path} = ...

        Once it had write accessors I for myself would switch to that module in favour of Class::Accessors which I currently had a closer look to in order to substitute an overblown Class::MethodMaker based module because that really pollutes my namespace.

        • I find your use of "Improve" interesting, mostly because it implies both that it would be better, and additionally that there is a need to make it better.

          For simple module that won't accumulate a large diversity of subclasses, I have no problem whatsoever writing to the hash element directly from within the same class that defines the property itself (You'll note I work directly with it in the new constructor as well).

          Also, that value can also be passed in directly.

          Module::CGI::Install->new( cgi_path =

          • I find your use of "Improve" interesting, [...]

            Please pardon my way of becoming personal. I originally wanted to show code that uses O::T to make it obvious what I mean and your module was the only one I found. I did not originally intend to let it sound as personal as it does, the way I phrased it in the end.

            To become more concrete, what I mean, is the following patch. Full patch with tests is at http://renormalist.net/misc/object_tiny_write_accessors.patch [renormalist.net].

            diff -ur Object-Tiny-1.03/lib/Object/Tiny.pm Object-Tiny-1.04/lib/Object/Tiny.pm
            --- Object-Tiny-1.03/lib/Object/Tiny.pm 2007-09-04 05:06:48.000000000 +0200
            +++ Object-Tiny-1.04/lib/Object/Tiny.pm 2007-09-07 12:03:29.000000000 +0200
            @@ -3,7 +3,7 @@
            # use strict; # Enable during dev and testing
            BEGIN {
                    require 5.004;
            -       $Object::Tiny::VERSION = '1.03';
            +       $Object::Tiny::VERSION = '1.04';
            }

            sub import {
            @@ -16,7 +16,7 @@
                            map {
                                    defined and ! ref and /^[^\W\d]\w*$/s
                                    or die "Invalid accessor name '$_'";
            -                       "sub $_ { return \$_[0]->{$_} }\n"
            +                       "sub $_ { if (defined \$_[1]) { \$_[0]->{$_} = \$_[1] } ; return \$_[0]->{$_} }\n"
                            } @_;
                    die "Failed to generate $pkg" if $@;
                    return 1;
            It is a naive implementation, but maybe it therefore even fits to the definition of "tiny".
            • I have two problems with that sort of change.

              1. It makes EVERY accessor writable, which is a BAD idea. I generally require that all objects are valid at all times, so allowing anything to arbitrarily change accessors is really really nasty.

              2. It makes the accessors slower (I think) because of the extra conditional.

              The change I was looking for was more for keeping the default read-only, but to add additional read/write accessors.