in reply to Re^2: Using a tied scalar as an in memory file
in thread Using a tied scalar as an in memory file

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 @_)

use strict; use warnings; { package TiedObject; sub TIESCALAR { my $sClass = shift; return bless([ @_ ], $sClass); } sub STORE {} sub FETCH { shift->[0]; } } my $sErr; tie $sErr, 'TiedObject', 5, "More data"; printTiedObject($sErr); sub printTiedObject { my $oErr = tied($_[0]); #does not copy scalar, prints out "...is tied to..." print "using \@_ directly\n"; if (defined($oErr)) { print "\$_[0] is tied to <$oErr>\n"; } else { print "\$_[0] is not tied"; } #copies scalar, prints out "..is not tied" print "\nafter \$param0 = shift \@_\n"; my $param0 = shift @_; $oErr = tied ($param0); if (defined($oErr)) { print "\$param0 is tied to <$oErr>\n"; } else { print "\$param0 is not tied\n"; } }

we get the following output

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

Best, beth

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

    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.

      Actually, I was talking about subs (though I realize now I may have confused you by using the word sub/function interchangably). From my original reply:
      When scalars are passed to a function, e.g. open they are copied (not passed by reference).

      I was wrong about them not being passed by reference to subs/functions (i.e. the calling convention), but not about them being copied, at least when people use the parameters the usual way. I was mistaken precisely because whenever I define subs I never use @_ directly. As a result, I formed a long standing misimpression of the calling convention. I simply didn't want others to make the mistake I did or to think that they could freely pass tied scalars into subs (or functions) without knowing how they were going to be used internally.

      As for "crying foul" - that makes this sound like a fight. I'm here to learn, not to argue. Perhaps those aren't your goals?

      In my opinion, trying to understand why someone made a mistake is as important as correcting them. And for me, it is an essential part of not repeating my own. I never was much of a rote learner.

      Best, beth

        Actually, I was talking about sub

        I agree that you were talking about subs, which is off-topic and misleading. Especially considering you specifically emphasised open which is not sub, and its arguments aren't passed to it by value.

        As for "crying foul" - that makes this sound like a fight.

        I don't know where you got that idea, but it's not the case.

        In my opinion, trying to understand why someone made a mistake is as important as correcting them.

        Indeed. And it had nothing to do with passing by value.

        Perhaps those aren't your goals?

        My goal was to help the OP, to which you were doing a disservice by providing the wrong explanation.