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

Oh wise monks of the symbol table,

I want to create a subroutine that alters a normal scalar into a tie'ed object, but without returning the object from the sub. This because I would like to use it as 'syntactic sugar' for a module interface.
# The idea is that when I do: use MyModule qw/my_sub/; my $scalar = 'something'; # $scalar is normal here my_sub($scalar); # but now has become a tie'd scalar
I figured i need to prototype the subroutine to implicitly get a reference to the var, so I declared it as sub my_sub (\$) {}. But now I wonder, whereto should I assign the tie'ed object, to the typeglob of the reference, to the typeglob of the scalar or just to the reference itself ?? And what is the right syntax to do this under use strict ?

--
Jaap Karssenberg || Pardus (Larus)? <pardus@cpan.org>
>>>> Zoidberg: So many memories, so many strange fluids gushing out of patients' bodies.... <<<<

Replies are listed 'Best First'.
Re: ref 'n typeglob
by diotalevi (Canon) on Apr 01, 2003 at 01:01 UTC

    You attempted to solve this wrong two ways and missed the one "right" way. First - my() variables are not accessible via the symbol table and so typeglobs have no place in this discussion. Second - you don't need the prototype because - Third - @_'s elements are aliased to the actual variables.

    In mysub tie the $_[0] variable directly. Don't shift it, don't pop it - you have to access it right inside the array.

      I knew there was something twisted in my mind ... *sigh*
      --
      Jaap Karssenberg || Pardus (Larus)? <pardus@cpan.org>
      >>>> Zoidberg: So many memories, so many strange fluids gushing out of patients' bodies.... <<<<

        Just as a light warning... diotalevi's method works fine but be aware that if you ever call my_sub("literal") (or similar), you will end up with a fatal error of Modification of a read-only value attempted at ... (with the file and line number being in your package at the tie; you may want to trap that error and carp it out to the caller).

        bbfu
        Black flowers blossom
        Fearless on my breath