lightoverhead has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks
Recently, I have done some reading regarding "Tie" in perl. And I have an question:
package Tie::Constvar; use Carp; sub TIESCALAR { my ($class, $initval) = @_; my $var = $initval; return bless \$var => $class; } sub FETCH { my $selfref = shift; return $$selfref; } sub STORE { confess "cannot assign value to a constant!"; } package main; tie my $AVO, Tie::Constvar, 6; print $AVO,"\n"; tie $AVO, Tie::Constvar, 3; #this works print $AVO,"\n"; $AVO = 3; #this will throw an exception! print $AVO,"\n";
As you can see that we cannot assign a value to "$AVO" as "$AVO = 3". This is expected. However, after I "Tied" $AVO to 6, I can "Tie" it again with 3 which didn't generate any error.
This made me wonder if Tying a variable is not considered "assignment".
So, how does the tied variable associate with its underlying returned object by "TIESCALAR"? It seems a hash like association. Is this right?
Does anyone have any insight on this? Thank you
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: question regarding "Tie" in Perl
by LanX (Saint) on Nov 04, 2013 at 20:05 UTC | |
by lightoverhead (Pilgrim) on Nov 04, 2013 at 20:22 UTC | |
by ww (Archbishop) on Nov 04, 2013 at 20:37 UTC | |
|
Re: question regarding "Tie" in Perl
by BrowserUk (Patriarch) on Nov 04, 2013 at 20:03 UTC | |
by lightoverhead (Pilgrim) on Nov 04, 2013 at 20:20 UTC | |
by BrowserUk (Patriarch) on Nov 04, 2013 at 20:25 UTC | |
by lightoverhead (Pilgrim) on Nov 04, 2013 at 20:31 UTC | |
by tobyink (Canon) on Nov 04, 2013 at 22:27 UTC | |
by BrowserUk (Patriarch) on Nov 05, 2013 at 01:49 UTC |