Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^4: Assignable Subroutines

by BrowserUk (Patriarch)
on Jan 25, 2005 at 15:54 UTC ( [id://424907]=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^5: Assignable Subroutines
by perrin (Chancellor) on Jan 25, 2005 at 16:03 UTC
    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.

        I dislike both syntaxes for assignment of values. I would suspect.. $spreadSheet->getCell('C1','C5')->setValue('123');

        Biggest reason to have the Cell as an object is that a cell isn't a string. It's an object that contains a value. In fact, in spreadsheet context, it has other attributes, such as formatting, margins and other sillinesses...

        ----
        Give me strength for today.. I will not talk it away..
        Just for a moment.. It will burn through the clouds.. and shine down on me.

      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 had the wierdest of thoughts (ain't TMTOWTDI wonderful?) ...

        sub update_thing { my ($self, $x, $y, $u) = @_; if (ref $u and UNIVERSAL::isa($u, 'CODE')) { # use temp to keep the $u function from changing $_[0] ;-) my $val = $self->{cell}[$x][$y]; $u = $u->($val); } # validate $u. if ($is_valid) { $self->{cell}[$x][$y] = $u; }

        called sorta like:

        $obj->update_thing(15000, 20000, sub { $_[0] =~ s[something][else]g; + $_[0] } );

        No, not better than $obj->thing(15000,2000) =~ s[something][else]g. But a bit better than the other ideas going around, maybe? :-) If not better, at least different...

        So it would seem you have answered my question: do the arguments get passed to both the setter and the getter or does only one of them get them. On reflection i suppose "both of them" is the obvious choice....

        ---
        demerphq

        How on earth did you reach that conclusion?

        It's confusing syntax. I thought you were assigning the return value of running the regex on the return value of your method call.

        You've never used substr( $buffer, $n, $m ) =~ s[this][that]g; ?

        No, I find that hard to read. I would use a temp variable there. Your alternative syntax example looks much clearer to me. I expect that the inefficient copying you're worried about would happen either way.

      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://424907]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found