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.
  • 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:

    • 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 accessors for instance variables has come up on ruby-talk from time to time, and I think you can get third party libraries that do this (presumably by overriding Class#initialize). The fact that there has never been a clamoring for it among the Ruby community ought to be telling, though.

        • 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.