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.
  • by djberg96 (2603) on 2006.12.10 10:44 (#52176) Journal

    I 've been trying to maintain some legacy code that badly needs updating. One of the main sins committed in this code is direct object access, so $obj->{attribute} = 'value', rather than $obj->set_attribute('value');
    This is one of those areas where Ruby's Uniform Access Principle demonstrates just how clunky Perl's OO and accessors really are. To wit:

    class Foo
       attr_accessor :attribute
    end

    obj = Foo.new
    obj.attribute = 'value'
    "But, wait!", you cry. "Accessing the attribute like that is an encapsulation violation!"

    You're not accessing the attribute directly. The syntax just makes it *look* like you are. In reality you're calling the Foo#attribute= method. This longhand example should clarify things:

       class Foo
          attr_reader :attribute
          def initialize
             @attribute = 7
          end
          def attribute=(value)
             puts "Hello World!"
          end
       end

       obj = Foo.new
       puts obj.attribute # 7
       obj.attribute = 99 # "Hello World!"
       puts obj.attribute # 7 - wasn't touched
    • def initialize
          @attribute = 7
      end

      This is one of those areas where Perl 6's Uniform Access Principle demonstrates just how clunky and non-uniform Ruby's instance attributes are.

      • I assume you mean the fact that Perl 6 provides default read-only (though configurable) accessors on attributes. That's what I get from Exegesis 12, anyway, unless something has changed.

        Personally, I think it's a misfeature, because I don't always want users to have access to the instance variables I'm using. I'm sure that's configurable, too, but I'd rather be explicit than implicit in this case. Or, just write a custom 'is' method that lets me imitate the Perl 6 syntax.

        The topic of autogenerating a

        • I actually meant that most of the Ruby code I've seen accesses instance variables directly within the class, despite the presence of an autogenerated accessor. There's a subclassing problem there. (Actually it's not only a problem for subclassing, but I use that as a convenient shorthand because I don't have a good way to say "allomorphic instance modification".)

          The fact that there has never been a clamoring for it among the Ruby community ought to be telling, though.

          I've never seen a clamoring for

          • I actually meant that most of the Ruby code I've seen accesses instance variables directly within the class, despite the presence of an autogenerated accessor. There's a subclassing problem there.

            I'm afraid I don't follow. Unless you have a custom writer, doing "self.attribute = 'value'" instead of "@attribute = 'value'" is a wasted method lookup. I fail to see what subclassing problem you would have.

            I've never seen a clamoring for a saner require either, but I don't take that as evidence that it's a particularly clear, clean, or well-defined feature.

            My only gripe with require is that it doesn't look in its own directory first, and so all the library authors end up doing $LOAD_PATH.unshift(File.dirname(__FILE__)) in each file. Otherwise, I'm not sure what you're referring to.

            • I fail to see what subclassing problem you would have.

              It's only a problem if a subclass wants to do something different with an attribute. I run into that once in a while.