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
    You have proven that tie doesn't suit here. =)

    I hope this code is only a sample for demonstration and not meant for production.

    IIRC do Perl variables have flags telling if they are blessed or tied and slots holding the bound class-names. (there is also untie, but no unbless)

    You might wanna read perlguts for details, I also recommend the (old) Panther book for internals.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      Rolf

      Thank you for your answer.

      By the way, what is IIRC?

        IIRC: If I Recall Correctly
Re: question regarding "Tie" in Perl
by BrowserUk (Patriarch) on Nov 04, 2013 at 20:03 UTC

    That's a horribly inefficient way of creating & using constants.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      This is just for question regarding "Tie" behavior.

      However, this implementation has advantage of using the tied variable as constant in a string which can be interpolated.

      "use constant AVO => 6" can not interpolate it in a string.

        However, this implementation has advantage of using the tied variable as constant in a string which can be interpolated.

        And the benefit of interpolating a compile-time constant into a string at runtime is?

        use constant AVO => 6; ... print "The constant is " . AVO . "\n";

        Or

        printf "The constant is %d\n", AVO; print "The constant is ${ \AVO }";;

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        That's no excuse for implementing a constant using tie.

        use strict; use warnings; Internals::SvREADONLY(my $answer = 42, 1); print "This interpolates: $answer\n"; $answer++; # dies
        use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name