jeanluca has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I just ran into an other problem. I've created (inside a sub) a tied variable (which is actually a connection with a DB). Now I would like to return the tied variable, but I'm returning its value..... here is a snippet of code
tie $mod,'ModName',{blabla} ; return $mod ;
How do I return the tied (object) and not its value (which comes from FATCH ) ??

Thanks a lot!!
Luca

Replies are listed 'Best First'.
Re: returning a tied variable
by jdporter (Paladin) on Mar 01, 2006 at 15:51 UTC

    I think you've got no choice but to return a reference to the tied scalar. E.g.

    tie $mod,'ModName',{blabla} ; return \$mod ;

    If you're looking for an alternative, one would be to tie a scalar which is passed to the sub (by ref) rather than tying a lexical which is created in the sub. But I'm not suggesting that that's a better way to go.

    We're building the house of the future together.
      That's convenient. I was afraid that would simply return a reference to the return value of tied($mod)->FETCH, but it does indeed return a reference to the tied variable. Thanks, Perl!

      Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
      How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
      Thnx, So I can conclude that it is not possible to make a copy of a tied variable (or object)!?

      Luca

        You didn't say you wanted to make a copy. Of course it's possible. But to help you do that, I think I'd have to see more of the function in question, and also something about the TIE class you're using.

        We're building the house of the future together.
Re: returning a tied variable
by ikegami (Patriarch) on Mar 01, 2006 at 16:33 UTC

    You can do it if the variable to tie is passed as an argument, since the elements of @_ are aliases (not copies) of the arguments.

    sub ModName::TIESCALAR { my $class = shift; my $self; return bless(\$self, $class); } sub test { tie $_[0], 'ModName'; } my $var; print(tied($var) ? 'tied' : 'not tied', "\n"); # not tied test($var); print(tied($var) ? 'tied' : 'not tied', "\n"); # tied

    Otherwise, you have to return it as a reference.

    sub ModName::TIESCALAR { my $class = shift; my $self; return bless(\$self, $class); } sub test { my $var; tie $var, 'ModName'; return \$var; } my $ref = test(); print(tied($$ref) ? 'tied' : 'not tied', "\n"); # tied

    You can't make a copy of a tied variable. You'll only be making a copy of the value its FETCH returns.

    sub ModName::TIESCALAR { my $class = shift; my $self; return bless(\$self, $class); } sub ModName::FETCH { } tie my $src, 'ModName'; print(tied($src) ? 'tied' : 'not tied', "\n"); # tied my $dst = $src; print(tied($dst) ? 'tied' : 'not tied', "\n"); # not tied
Re: returning a tied variable
by Anonymous Monk on Mar 01, 2006 at 16:19 UTC
    If you declare
    sub x:lvalue{ ... ;$tied} #last expression, no "return"
    then you are "copying" (after a fashion) the var. Thats at least what the docs say, I haven't tried it.