June 26, 2008
Dee Girl wrote:

> Steven Schveighoffer Wrote:
> 
> > 
> > "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?
> > > 
> > > 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.
> > 
> > No it's not.  The only conclusive proof comes from observing what happens when you copy strings from one to the other:
> > 
> > #!/usr/bin/perl
> > 
> > print "before x\n";
> > sleep 20;
> > $x = 'x' x 100000000;
> > print "initialized x\n";
> > sleep 5;
> > $y = $x;
> > print "copied to y\n";
> > sleep 5;
> > substr $x, 3, 1, 'y';
> > print "did substring\n";
> > print substr $y, 0, 5;
> > print "\n";
> > sleep 5;
> > 
> > OK, so what does this do?  I set x to a string of 100 million x's, then assign x to y, then replace the 4th character in x with a 'y', then print the first 5 characters of y to see if they changed too (see if x and y reference the same data)
> > 
> > So what does this output?
> > before x
> > initialized x
> > copied to y
> > did substring
> > xxxxx
> > 
> > But this is not yet conclusive proof, we need to watch what happens with memory usage when each step occurs (hence the sleeps).  So using 'top', I observed this:
> > 
> > before x => mem usage 3k
> > initialized x => 191MB (!)
> > copied to y => 291MB
> > did substring => 291MB
> > xxxxx
> > 
> > So, what it looks like is on assignment, the string is copied, and the editing edits the string in-place.   But I can't really explain why it takes 191MB to store x, where it only takes 100MB to store y.
> > 
> > So I'd say perl does not have invariant strings.  'course, I'm not a perl hacker, so I don't know if I did this correctly :)
> > 
> > -Steve
> 
> Hello! I think the first part of your message is correct. the second is maybe mis guided. Walter is correct. Perl strings do not have mutable chars. They can be think as similar to D strings. Your example with $x and $y shows that.
> 
> Perl can optimize copies sometimes. But it does not matter. Semantics is all that matters. And Perl strings can not mutate individual chars ever. Thanks, Dee Girl

Utter baloney.

b.

-- 

June 27, 2008
Me Here Wrote:

> Simen Kjaeraas wrote:
> 
> > Me Here <p9e883002@sneakemail.com> wrote:
> > > 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.
> > 
> > Could not the garbage collector theoretically be intelligent enough to see that there's only one reference to the string, and thus not do CoW?
> > 
> > -- Simen
> 
> Perhaps you will find this a more convincing demonstration:
> 
>     [0] Perl> $s = 'the quick brown fox';;
>     [0] Perl> $r = \substr $s, 10, 5;;
>     [0] Perl> $$r = 'green';;
>     [0] Perl> print $s;;
>     the quick green fox

I stand corrected. As could your manners ^_^.

This is good example. Trick of taking a reference to result of substr is new to me. Maybe now D is more better compared to Perl because in D you can not make aliased changes to string! Thanks for teaching me. Dee Girl
June 27, 2008
Me Here Wrote:

> Simen Kjaeraas wrote:
> 
> > Me Here <p9e883002@sneakemail.com> wrote:
> > > 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.
> > 
> > Could not the garbage collector theoretically be intelligent enough to see that there's only one reference to the string, and thus not do CoW?
> > 
> > -- Simen
> 
> Perhaps you will find this a more convincing demonstration:
> 
>     [0] Perl> $s = 'the quick brown fox';;
>     [0] Perl> $r = \substr $s, 10, 5;;
>     [0] Perl> $$r = 'green';;
>     [0] Perl> print $s;;
>     the quick green fox

I stand corrected. As could your manners ^_^.

This is good example. Trick of taking a reference to result of substr is new to me. Very interesting even when I made $$r = "very long string" x 100 it still works. The number of characters can be different. How they do it?

Maybe now D is more better compared to Perl because in D you can not make aliased changes to string! Thanks for teaching me. Dee Girl
June 27, 2008
Dee Girl wrote:

> Me Here Wrote:
> > Perhaps you will find this a more convincing demonstration:
> > 
> >     [0] Perl> $s = 'the quick brown fox';;
> >     [0] Perl> $r = \substr $s, 10, 5;;
> >     [0] Perl> $$r = 'green';;
> >     [0] Perl> print $s;;
> >     the quick green fox
> 
> I stand corrected. As could your manners ^_^.

Sorry if you find my directness to be 'rude', but making assumptions based on your guesswork about trivial eveidence and then propounding conclusions as if they were facts, does not engender me to de indirect.

> 
> This is good example. Trick of taking a reference to result of substr is new to me. Very interesting even when I made $$r = "very long string" x 100 it still works. The number of characters can be different. How they do it?

It wasn't easy. But, if you look back to the earlier thread where I demonstrated the in-place insertions and deletions are achieved through the use of a pointer, length and offset, it should be clearer.

> 
> Maybe now D is more better compared to Perl because in D you can not make aliased changes to string! Thanks for teaching me. Dee Girl

Welcome, b.

-- 

June 27, 2008
Me Here Wrote:

> Dee Girl wrote:
> 
> > Me Here Wrote:
> > > Perhaps you will find this a more convincing demonstration:
> > > 
> > >     [0] Perl> $s = 'the quick brown fox';;
> > >     [0] Perl> $r = \substr $s, 10, 5;;
> > >     [0] Perl> $$r = 'green';;
> > >     [0] Perl> print $s;;
> > >     the quick green fox
> > 
> > I stand corrected. As could your manners ^_^.
> 
> Sorry if you find my directness to be 'rude', but making assumptions based on your guesswork about trivial eveidence and then propounding conclusions as if they were facts, does not engender me to de indirect.

I do not like to do that. It was from the Perl book which I read from cover to back cover. But it has been some time and clearly I do not remember all correct. Thank you, Dee Girl
June 27, 2008
Dee Girl wrote:

> Me Here Wrote:
> 
> > Dee Girl wrote:
> > 
> > But it has been some time and clearly I do not remember all
> correct. Thank you, Dee Girl

There are a lot of perl books and much that is not written about in most of them. It is easy to infer the wrong thing based upon what you see.

I know, because I make that mistake all the time :)

Here's another little snippet that might amuse you:

	$s = '1234';
	$r1 = \vec( $s, 1, 8 );
	$r2 = \vec( $s, 2, 8 );
	$$r1 ^= $$r2 ^= $$r1 ^= $$r2;

	print $s;;
	1324

Cheers, b.

-- 

1 2 3 4
Next ›   Last »