in reply to Using a tied scalar as an in memory file

And there is yet a third problem with tied scalars. When scalars are passed to a function, e.g. open they are copied (not passed by reference).

Ties do not propagate after a copy, hence the scalar, but neither the tied object nor its associated sub are being passed. The reason you are seeing the correct thing when you print is that when a tied variable is copied, whatever is returned by FETCH is assigned to the variable on the left. Last night I had the similar problem as you when I tried to pass a tied variable to die - which also strips the tied portion from the scalar and throws only the value returned by FETCH

To quote from Programming Perl:

The tie is on the variable, not the value, so the tied nature of a variable does not propagate across assignment. For example, let's say you copy a variable that's been tied:

$dromedary = $camel;

Instead of reading the value in the ordinary fashion from the $camel scalar variable, Perl invokes the FETCH method on the associated underlying object. It's as though you'd written this:

$dromedary = (tied $camel)->FETCH():

Best, beth

Replies are listed 'Best First'.
Re^2: Using a tied scalar as an in memory file
by ikegami (Patriarch) on Feb 20, 2009 at 14:56 UTC

    When scalars are passed to a function, e.g. open they are copied (not passed by reference).

    That's completely wrong.

    • Arguments are not copied. Perl always passes by reference.
    • The scalar isn't being passed to a function — a reference to it is — so it's moot whether the arg is passed by ref or by val.
      Once again, you have correctly corrected me. :-) Thanks. I didn't happen to notice that OP was passing a reference, so, as you say, the issue was moot. On the other hand, whilst you are right that Perl passes by reference and I am grateful for the correction, the usual semantics of parameter usage does involve copies that can clobber a passed in tied scalar:

      In this code sample, which compares the usage of a parameter array element directly ($_[0]) with one common usage of the array ($param = shift @_)

      we get the following output

      using @_ directly $_[0] is tied to <TiedObject=ARRAY(0x814ed48)> after $param0 = shift @_ $param0 is not tied

      Best, beth

        You thought the variable was being passed directly, so you believed the SV was being passed by reference. In the same breath, you said tied variables don't work because SVs are passed by value. I called fouled on the latter, so I don't see how the typical practices of subs are of any consequence here.

        Furthermore, we weren't talking about subs, so the typical practices for writing subs are completely irrelevant. (Builtin and other XS) functions access the SV directly until a specific native type is needed. I don't think I've ever seen one that copies the arguments on entry. Remember, the primary purpose of copying the args in subs is to provide named parameters, but functions already have named parameters.

Re^2: Using a tied scalar as an in memory file
by duncs (Beadle) on Feb 20, 2009 at 09:24 UTC

    Thanks for all the replies - I guess I'll give up now and rethink my strategy...

    Duncs