in reply to two-part question about tied variables

Well, you could use my (as yet unpublished) Loop::Watch module, which allows you to write code like:
use Loop::Watch; my $x = 2; ensure { $x > 0 } watching($x), doing { # all your code centered around $x here };
Don't worry -- that's not a loop. The doing() construct means "do this once", as opposed to looping().

There are probably some assertion modules out there, and mine is one such module.

As for your question about passing the value, the answer is to not use tie(). "Huh?" Well, don't have your interface use tie(). Have it use some auxiliary function that calls tie() with the appropriate duplicated value. Here's how I would write the application.

package Tie::Scalar::Watch; use Carp; use strict; # rich man's Exporter ;) # this just puts 'watch()' in the caller's namespace sub import { my $pkg = caller; no strict 'refs'; *{ $pkg . "::watch" } = \&watch; } sub TIESCALAR { my ($class, $val, $rule) = @_; my $self = bless [ undef, $rule ], $class; $self->STORE($val); return $self; } sub FETCH { $_[0][0] } sub STORE { my ($self, $val) = @_; return $self->[0] = $val if $self->[1]->($val); croak "Value $val is out of bounds"; } sub watch (&$) { tie $_[1], __PACKAGE__, $_[1], $_[0]; } 1;
Above is the module responsible for keeping track of the scalar. Now is the code that uses it.
use Tie::Scalar::Watch; # imports 'watch' my $x = 10; watch { $_[0] > 5 } $x; # $x is now transparently tied! # and you thought tying was transparent already! while (1) { print "$x\n"; $x--; }
That code doesn't run forever -- it prints 10, 9, 8, 7, and 6, and then dies with Value 5 is out of bounds at ... line 11.

If you want to know how to write the code such that you can say watch { $foo > 5 } $foo instead of using the "ugly" $_[0] notation, here's how:

sub STORE { my ($self, $val) = @_; $self->[0] = $val; $self->[1]->() ? return $val : croak "Value $val is out of bounds"; }
Sneaky, no? That's all for now. I hope this helps you out.

_____________________________________________________
Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Replies are listed 'Best First'.
Re: Re: two-part question about tied variables
by seattlejohn (Deacon) on Jan 13, 2002 at 21:07 UTC
    Japhy, your code does just about everything I was hoping for and then some... it's a really slick solution. Thanks!
Re2: two-part question about tied variables
by dragonchild (Archbishop) on Jan 14, 2002 at 19:31 UTC
    Shouldn't you have a use 5.6.0; at the top of your Tie::Scalar::Watch? watch(&$) won't work as advertised in 5.0.x, will it?

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      It works in 5.005, at least. And in this day and age, I'd hope people are at least using 5.005.

      _____________________________________________________
      Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
      s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;