Generic const - a non-functional view

Me Here p9e883002 at sneakemail.com
Thu Jun 26 08:06:59 PDT 2008


Bill Baxter wrote:

> Me Here wrote:
> > Walter Bright wrote:
> > 
> > Perl has invariant strings, but they are implicitly invariant
> > > and so nobody notices it, they just work.
> > > 
> > Sorry Walter, but thta is simply not the case: Vis:
> > 
> >[0] Perl> $x = 'x' x 500e6;
> >[0] Perl> print length $x;;
> > 500000000
> >[0] Perl> substr $x, 250e6, 1, 'y';;
> >[0] Perl> print length $x;;
> > 500000000
> >[0] Perl> print substr $x, 250e6-5, 10;;
> > xxxxxyxxxx
> > 
> > b.
> 
> What are you disagreeing with?

Simple. Walter's statement that Perl's strings are invarient. 

> 
> The fact that they're invariant?
> Or the fact that nobody notices?
> 
> I have no idea if they're invariant in perl or not.  But I don't think your
> test above is conclusive proof that they're mutable.
> 

True. It is not conclusive proof. That's very hard to provide in a forum such
as this.

I *know* the above proves it, because I can monitor the memory usage and
addresses.
I used a very large string and the mutated a character in the middle of it. If
the original string was mutated, the memory consumption of the process would
have to (breifly) double. It does not.

For those familiar with Perl, a simple 

    $scalar =~ s[(.)(.)][\2\2]g;

would be sufficient to demonstrate it. This will swap every pair of characters
in the string, in place.

Or 
    $scalar =~ tr[A-Za-z][a-zA-Z];

which will switch the case of all characters in the string *in-place*.

So, let me make this statement: Perl's SVs are *absolutely not* immutable!

> --bb

What does this mean for the wider discussion? Nothing. But that particular part
of Walter's post is incorrect.

b.
-- 




More information about the Digitalmars-d mailing list