[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[FWP] awww, no fun :(



I was trying to create a hybrid while loop -- one that ends the instant a
condition is true -- and I was most displeased with Perl's magic.  When a
tie()d variables functions get called (STORE, FETCH, etc.), the stack gets
a little silly. :(

#########################################################################
#!/usr/bin/perl

package Tie::Ensure;
use strict;


sub TIESCALAR {
  my ($class, $var, $code) = @_;
  bless {
    VAL => $$var,
    CODE => $code,
  }, $class;
}


sub FETCH { $_[0]{VAL} }


sub STORE {
  my ($self, $val) = @_;
  local $_ = $self->{VAL} = $val;
  $self->{CODE}->() ? $_ : last;
}


package main;
use strict;


sub ensure (&@) {
  my ($cref, $obj, $loop) = @_;
  tied($$obj)->{CODE} = $cref;
  { $loop->(); redo }
}


sub using (\$) {
  tie ${$_[0]}, 'Tie::Ensure', $_[0];
  return $_[0];
}


sub looping (&) { $_[0] }


my $foo = 10;

ensure { $_ < 15 } using($foo), looping {
  print "before: $foo\n";
  $foo++;
  print "after: $foo\n";
};
#########################################################################

I get told that I can't call last().  That's upsetting.  Does anyone know
how to emulate the effect, other than change $foo++ to

  tied($foo)->STORE($foo + 1);

(which works, by the way).

-- 
Jeff "japhy" Pinyan      japhy@pobox.com      http://www.pobox.com/~japhy/
Are you a Monk?  http://www.perlmonks.com/     http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc.     http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter.         Brother #734


==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe