I think the aversion to using lvalue subroutines is cultural

Not in my case :-) I came to perl after doing a lot of coding in Pop-11 and I still miss its "updaterof" idiom.

I would like being able to do $o->foo = 12 in my perl modules. It makes it explicit when a method is a mutator without the noise of having separate setter/getter functions.

Unfortunately (in my opinion) the perl implementation of lvalue subs is near useless since it exposes the object implementation - making it harder to change your module

Yes, you can put in bad data, yes you can break things, but putting rubber pads on the edge of all of your functions does not make them safe.

Nothing can make your code 100% safe :-) Having assertions on arguments, etc. can certainly make it a lot safer. It can cut development time on larger projects by a silly amount - because you get sane errors close to the location that caused them.

It's just defensive programming, which in many cases is dangerous if over-done.

I agree, but I see it under-done a lot more than I see it over-done.

However, the point I was trying to make wasn't about checking arguments - it was about exposing the implementation of your class.

For example, consider a hypothetical object that can be readable, writeable or locked. With an :lvalue based API you would have something like this:

my $o = Foo->new; $o->locked = 1; $o->writeable = 0; $o->readable = 1;

Now, what happens if (for whatever reason) I want to change the underlying implementation of these attributes to use a bitmask. You either have to change your API - or create some ghastly hack with tie. This is what I mean by an over-exposure of your object's implementation.

With a traditional single accessor/mutator or separate setter/getter methods - no problem.

As far as I can tell, lvalue works fine now, and when it's replaced with something better in Perl 6, which surely it will be, then there's no reason not to upgrade.

Hmmm... I wonder if you could do something sane now with the aid of tie and some source filters.... I'll have to have a think when I get some spare time...

One could just as easily argue that Perl 5 is experimental. Your points 1, 2, and 3 all apply to the same degree.

Got to disagree with that completely. They do not apply to the same degree. Experimental features break on a regular basis (threads, some of the regex stuff, etc.). Core perl has been very solid - due to lots of care and attention by the wonderful development peeps. I've had perl4 scripts run on 5.8 without a single change.

The 5.8 release especially has had a lot of time spent on a very nice test suite to prevent regression problems.


Update: Cleaned up the English a little.


In reply to Re^2: $foo->bar = 14; by adrianh
in thread $foo->bar = 14; by Juerd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.