Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^3: Assignable Subroutines

by perrin (Chancellor)
on Jan 25, 2005 at 15:51 UTC ( [id://424906]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Assignable Subroutines
in thread Assignable Subroutines

How is it different, aside from syntax, from calling a setter method?

Replies are listed 'Best First'.
Re^4: Assignable Subroutines
by BrowserUk (Patriarch) on Jan 25, 2005 at 15:54 UTC

    How do you do this with a setter method?

    $obj->thing( 15000, 20000 ) =~ s[something][else]g;

    Examine what is said, not who speaks.
    Silence betokens consent.
    Love the truth but pardon error.
      I can't tell what that's supposed to do. If it's just combining two setters into one call, I would do this:
      $obj->thing( a => 1, b => 2);
        $obj->thing(15000, 20000) =~ s[something][else]g;

        might be easier to read as:

        $spreadsheet->cell('C3', 'D4') =~ s[something][else]g;

        There has to be a clean, and I mean absolutely clean, way of doing this - which P6 is either giving to us, or will get us very close to. What we have in P5 is not even close:

        # current lvalue sub: (my $val = $spreadsheet->cell('C3', 'D4')) =~ s[something][else]g; $spreadsheet->cell('C3', 'D4') = $val; $spreadsheet->recalc() if $spreadsheet->autorecalc(); # without lvalue sub: (my $val = $spreadsheet->cell('C3', 'D4')) =~ s[something][else]g; $spreadsheet->cell('C3', 'D4', $val);

        A world of difference syntactically in the problem domains where this makes sense. With the current lvalue sub, you can't hook in after the assignment to do things (such as validation, or, as above, recalculating the sheet). Without the lvalue sub, we can do this, but syntactically it's a bit more awkward.

        If it's just combining two setters into one call, ...

        How on earth did you reach that conclusion?

        This syntax is standard perl and works now. Is my use of [] so confusing?

        You've never used

        substr( $buffer, $n, $m ) =~ s[this][that]g;
        ?

        Note, this might be direct access to an internal buffer, but it could equally be a subset of a bigger than memory file, or a BLOB queried from a DB.

        The problem is that, you cannot do anything afterwards.

        So, for example, in the latter case, you could not arrange for the modified BLOB to be written back to the DB.

        Of course, the alternative syntax is:

        my $temp = $obj->get_thing( 15000, 20000 ); $temp =~ s[this][that]g; $obj->set_thing( 15000, 200000, $temp );

        which besides the:

        1. inconvenient notation;
        2. requirement for a temporary variable;
        3. the inefficiency of copying large chunks of data around unnecessarially.
        4. The need for two nearly identical pieces of code.
        5. and the potential for someone to come along and stick some other lines of code between two halves of what should be an "atomic" operation.

        Did you notice the typo?


        Examine what is said, not who speaks.
        Silence betokens consent.
        Love the truth but pardon error.

        I think you're missing the point, the pseudo-code id consider more or less equivelent to what BrowserUK wrote is:

        #$obj->thing( 15000, 20000 ) =~ s[something][else]g; { my $tmp=$obj->thing(); $tmp =~ s[something][else]g; $obj->thing(15000,20000,$tmp); }

        I suspect we could debate about exactly how this one should perform (omitting the arguments its easy but whether the arguments belong to the setter, the getter or both is a debate probably worth having.)

        ---
        demerphq

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://424906]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2024-04-20 04:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found