Skip Menu |

This queue is for tickets about the Tie-Constrained CPAN distribution.

Report information
The Basics
Id: 114717
Status: open
Priority: 0/
Queue: Tie-Constrained

People
Owner: Nobody in particular
Requestors: ww6442 [...] yp.com
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: Passing variables to subs
Date: Wed, 25 May 2016 23:32:36 +0000
To: "bug-tie-constrained [...] rt.cpan.org" <bug-tie-constrained [...] rt.cpan.org>
From: Bill Wagner <ww6442 [...] yp.com>
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? Thanks, Bill
On Wed May 25 19:32:51 2016, ww6442@yp.com wrote: Show quoted text
> 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.
Subject: Re: [rt.cpan.org #114717] Passing variables to subs
Date: Thu, 26 May 2016 17:32:37 +0000
To: "bug-Tie-Constrained [...] rt.cpan.org" <bug-Tie-Constrained [...] rt.cpan.org>
From: Bill Wagner <ww6442 [...] yp.com>
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
On Thu May 26 13:32:55 2016, ww6442@yp.com wrote: Show quoted text
> 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 forgot to mention that you can also write for my $killer ($_[0]) { } but if the entire sub body is inside a for loop, you will need an explicit return, if you are not already using return.
Subject: Re: [rt.cpan.org #114717] Passing variables to subs
Date: Fri, 27 May 2016 17:25:31 +0000
To: "bug-Tie-Constrained [...] rt.cpan.org" <bug-Tie-Constrained [...] rt.cpan.org>
From: Bill Wagner <ww6442 [...] yp.com>
On 5/26/16, 2:48 PM, "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=Z6XhTWyW62a-9HA78HXo_bPBQ41LoAh5p8XC5lDb0kQ&s=0rBMvYb2zCtVi7v4EX89DdQFms9mEOqnCbO57M5L2mM&e= > > >On Thu May 26 13:32:55 2016, ww6442@yp.com wrote:
>> 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 forgot to mention that you can also write > > for my $killer ($_[0]) { > > > } > >but if the entire sub body is inside a for loop, you will need an explicit return, if you are not already using return.
Cool! It’s a bit inconvenient to have to do that, but I think it does solve my problem, and it’s not too onerous for me to use in order to get the benefits of automatically reconnecting. Thanks again for your help with this! Bill