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:??;


In reply to Re: two-part question about tied variables by japhy
in thread two-part question about tied variables by seattlejohn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.