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

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

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

    It's a fine notion but it has it's problems.

    You now, need to code

    • a get_thing() method;
      print $obj->thing;
    • a set_thing() method;
      $obj->thing = 'someting';
    • a update_thing() method;
      $obj->thing =~ s[this][that]g;
    • a return_list_of_matched_subthings() method;
      my @subthings = $obj->thing =~ m[subthing]g;
    • a return_count_of_subthings() method;
      my $count = $obj->thing =~ m[subthing]g;
    • a get_subthing_iterator() method;
      while( $obj->thing =~ m[subthing]g ) { last if substr( $obj->thing, pos( $obj->thing ) - 22, 22 ) eq 'This is *the* subthing'; }

      (Oh! And a substr_thing() method;)

    • a preincr_thing() method;
      $obj->thing++;
    • A postincr_thing() method;
      ++$obj->thing;
    • (pre/post decr etc. etc)

    And all the others that you effectively get for free with a single lvalue method.

    All that is missing is a convenient way to validate the assignment.


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