I dug for an hour or so into the concept of using a set of tied scalars whos "STORE" methods only store references to $_[1], so that copies are never made. My goal was to encapsulate all use of references within the module that handles the tied scalars, so that the scalars themselves just look like normal plain-vanilla scalars, and yet they never actually create a copy of the original data (they should only internally store references to the original data).

Unfortunately, my attempts, while producing functional code, seem to still make copies. diotalevi suggests that this is because the ( $x, $y ) = ( $y, $x ) construct has its own optimizations that create the copies internally no matter what you do. I also suspect that my FETCH method's ${$self->{VRef}} construct is creating a copy.

So my attempt at a pure Perl solution to swapping the values of two lexically scoped scalars without creating (explicitly or internally) copies, and without explicitly using references in the main code, is a failure.

Nevertheless, I thought that the attempt was worth demonstrating to see if anyone else could do anything with it. I really thought I was on to something, but it seems that there isn't a pure-Perl solution to the OP's quandry. Before I paste it here, I just wanted to quickly thank blokhead for helping me test it. Here it is:

package Tie::Scalar::NoCopies; # This package compiles, runs, and implements a tied # scalar. But it doesn't do as it advertises... ie., # it doesn't suppress copies. use strict; use warnings; sub TIESCALAR { my ( $class ) = @_; my $self = {}; $self->{VRef} = undef; bless $self, $class; } sub STORE { my $self = shift; $self->{VRef} = \$_[0]; return ${$self->{VRef}}; } sub FETCH { my $self = shift; return ${$self->{VRef}}; } sub DESTROY { my $self = shift; } 1; package main; use strict; use warnings; my ($var1, $var2); tie $var1, "Tie::Scalar::NoCopies"; tie $var2, "Tie::Scalar::NoCopies"; $var1 = 'a' x 10; $var2 = 'b' x 10; print "$var1\t$var2\n"; ( $var1, $var2 ) = ( $var2, $var1 ); print "$var1\t$var2\n";

Enjoy!!!


Dave


In reply to Re: How to swap scalar values without copies by davido
in thread How to swap scalar values without copies by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.