On 5/26/16, 12:12 AM, "Father Chrysostomos via RT" <bug-Tie-Constrained@rt.cpan.org> wrote:
Show quoted text><URL:
https://urldefense.proofpoint.com/v2/url?u=https-3A__rt.cpan.org_Ticket_Display.html-3Fid-3D114717&d=CwIDaQ&c=lXkdEK1PC7UK9oKA-BBSI8p1AamzLOSncm6Vfn0C_UQ&r=mSIKxTzY2hUrAMN-rN7YOg&m=Aji7EcgrChBkpdH6_SQTVus4-TgWIt25bQlm-ABppvM&s=LC-KSdlaaCipYLKT25bBgPdI-NhsEs86cSVhL-UQ-2E&e= >
>
>On Wed May 25 19:32:51 2016, ww6442@yp.com wrote:
>> Hi,
>>
>> I think I may have run into a bug with your Tie::Constrained module.
>> If I try to pass a tied variable to a sub, then use the variable in
>> that sub, it seems that it no longer has the automatic validation and
>> fail handling features. For example:
>> #!/usr/bin/perl
>>
>> use Tie::Constrained;
>>
>> sub mytest($) {
>> my ($killer) = @_;
>>
>> $killer = "now is the time for all good men";
>>
>> print "Value of killer: $killer\n";
>>
>> }
>>
>> tie my $cons_killer, Tie::Constrained =>
>> sub { $_[0] =~ tr/aeiouy//cd; 1 };
>> $cons_killer = "googleplex";
>>
>> print "Value of cons_killer: $cons_killer\n";
>>
>> mytest($cons_killer);
>>
>> $cons_killer = "now is the time for all good men";
>>
>> print "Value of cons_killer: $cons_killer\n";
>>
>> Running this outputs:
>> Value of cons_killer: ooee
>> Value of killer: now is the time for all good men
>> Value of cons_killer: oieieoaooe
>>
>> Is there something that I should be doing differently that would
>> enable me to retain the features of your module when I pass the
>> variable to a sub?
>
>(I am not the author of the module. I just happened to notice this ticket.)
>
>The ‘my ($killer) = @_;’ line copies the value into $killer, so $killer is not tied. $_[0] is.
>
>If you are using a recent Perl (5.22 or higher), you can make $killer an alias to $_[0] like this:
>
> use experimental 'refaliasing';
> \$killer = \$_[0]; # or (\$killer) = \(@_);
>
>and it will behave as you expect, because $killer will be just another name for the caller’s variable.
>
>Alternatively, you could try using Variable::Magic and applying copy magic to the scalar so that the tie gets copied, but that’s a huge amount of work.
>
Thanks for your suggestion, but the code that I will be using this for is a production system, so I don’t think that I will be able to take advantage of the experimental feature. I’m not familiar with Variable::Magic, but I will take a look at it.
To give some context, what I am actually using this for is database handles similar to what the module’s author posted here:
http://www.perlmonks.org/?node_id=429281. The current code that I’d like to modify to use Tie::Constrained passes database handles around to various subs and it would be great if they could retain the validation and automatic reconnect features. Perhaps if the module’s author sees this, he could advise on how I should proceed.
Thanks,
Bill