<?xml version="1.0" encoding="windows-1252"?>
<node id="498434" title="Re: lhs substr(): refs vs. scalars" created="2005-10-08 13:06:53" updated="2005-10-08 09:06:53">
<type id="11">
note</type>
<author id="171588">
BrowserUk</author>
<data>
<field name="doctext">
&lt;blockquote&gt;&lt;i&gt;&lt;/i&gt;&lt;/blockquote&gt;
&lt;p&gt;You know you can use [substr] as an lvalue or with a fourth parameter to avoid duplicating your big scalars. 

&lt;p&gt;If you need to use [unpack] to decode small chunks of the buffer, or [pack] to overwrite small chunks, use them in conjunction with [substr] to avoid copying:

&lt;code&gt;
my @decoded = unpack '...', substr $bigscalar, $offset, $size;

substr $bigscalar, $offset, $size, pack '...', @newValues;

# or
substr( $bigscalar, $offset, $size ) = pack '...', @newValues;
&lt;/code&gt;

&lt;p&gt;But be very sure that the size specified in substr, and the size of the result from [pack] match exactly, otherwise you will be expanding or shrinking your big scalar by the difference  which will lead to nasty surprises. It may be better to use an intermediary variable here:

&lt;code&gt;
my $replacement = pack '...', @newValues;

substr( $bigScalar, $offset, length $replacement ) = $replacement;
&lt;/code&gt;

&lt;p&gt;It is also possible (from 5.8.5 onwards) to set up an array of lvalue references to chunks of your scalar and then manipulate the individual chunks through indirection:

&lt;code&gt;
## Create a scalar
perl&gt; $bigScalar = 'the quick brown fox jumps over the laxy dog';;

## Create an array of lvalue refs to the indivdual words using \substr...
perl&gt; @lvrefs = map{ 
    \substr $bigScalar, $_-&gt;[0], $_-&gt;[1] 
} [0,3], [4,5], [10,5], [16,3], [20,5], [26,4], [31,3], [35,4], [40,3];;

## Indirecting through the elements of the array gives you the words
perl&gt; print $$_ for @lvrefs;;
the
quick
brown
fox
jumps
over
the
laxy
dog

## And assign through the elements allows you to replace them, in-place, individually
perl&gt; ${ $lvrefs[7] } = 'lazy';; ## The ${ ... } is necessary.

## The typo is now corrected.
perl&gt; print $bigScalar;;
the quick brown fox jumps over the lazy dog
&lt;/code&gt;

&lt;div class="pmsig"&gt;&lt;div class="pmsig-171588"&gt;
&lt;hr /&gt;
&lt;font size=1 &gt;
&lt;div&gt;Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.&lt;/div&gt;
&lt;div&gt;Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?&lt;/div&gt;
&lt;div&gt;"Science is about questioning the status quo. Questioning authority". &lt;/div&gt;
&lt;div&gt;The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.&lt;/div&gt;
&lt;/font&gt;

&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
498428</field>
<field name="parent_node">
498428</field>
</data>
</node>
