in reply to Re^2: Perl OO and accessors
in thread Perl OO and accessors

what would you do with $c->area = -42?

What you do with $c->set_area( -42 )?

how do you go back and change the interface later to avoid lvalue subs?

How do you go back and change the interface later to avoid set_foo methods?

Makeshifts last the longest.

Replies are listed 'Best First'.
Re^4: Perl OO and accessors
by sauoq (Abbot) on Nov 29, 2005 at 04:32 UTC
    What you do with $c->set_area( -42 )?

    Maybe I'm missing something. Say that I implemented set_area as

    sub set_area { my $self = shift; my $area = shift; die "Area must be non-negative." unless $area >= 0; # ... }
    How do you do the same with an lvalue sub?

    How do you go back and change the interface later to avoid set_foo methods?

    You don't. But, that's okay because you never have a need to.

    -sauoq
    "My two cents aren't worth a dime.";
    

      How do you do the same with an lvalue sub?

      You return a tied value. Ugly, slow, and doable. fergal did say <q>Neither of these expose an attribute, despite how it looks.</q>

      You don’t. But, that’s okay because you never have a need to.

      Why then would I ever have a need to go back and avoid lvalue subs?

      Makeshifts last the longest.

        As Ovid once put it:

        Of course, some lvalue fans argue that I can use tie to get my validation back. Great! Now I've lost much of the conciseness I've hoped to gain and I've probably moved the data validation code away from the method in which the maintenance programmer expects to see it but at least I've switched to a slow, confusing and fragile interface!

        -sauoq
        "My two cents aren't worth a dime.";
        

        I was going to /msg you with "and don't use tie" but you beat me to it.

        You return a tied value. Ugly, slow, and doable.
        Why then would I ever have a need to go back and avoid lvalue subs?

        Perhaps when you decide you need validation without an ugly and slow (but doable) implementation?

        -sauoq
        "My two cents aren't worth a dime.";