![]() |
|
Your skill will accomplish what the force of many cannot |
|
PerlMonks |
Returning a tied scalarby MarkusLaker (Beadle) |
on Apr 16, 2005 at 10:46 UTC ( #448441=perlquestion: print w/replies, xml ) | Need Help?? |
MarkusLaker has asked for the wisdom of the Perl Monks concerning the following question:
A colleague asked if there was a way to make a Perl program die as soon as a particular variable became undefined. There's a more general question waiting to be asked: can we construct a variable that throws an exception as soon as you assign a value that doesn't meet some user-provided condition?
It turns out that we can. Here's a proof of principle:
I don't like the way you have to declare a checked variable:
It hides the declaration of the new variable inside a function argument, and the assignment is completely unintuitive. Even worse, if you forget the backslash, the code silently does the wrong thing. I'd prefer to be able to say:
Trouble is, the hypothetical method CheckFactory::Make can't return a tied variable: if it tries, the caller will get a copy that isn't tied, and the tied variable will be destroyed when the method exits. So is there a way around this? Can I somehow return a tied variable from a sub? Many thanks, Markus
Back to
Seekers of Perl Wisdom
|
|