bug, feature, or what ??

31 views
Skip to first unread message

Larry Wall

unread,
Oct 29, 1990, 6:24:31 PM10/29/90
to
In article <41...@ruuinf.cs.ruu.nl> he...@ruuinf.cs.ruu.nl (Henk P. Penning) writes:
: # Why is it that:
:
: @row = 1..8 ; print join(',',@row), ".\n" ;
: $#row = 3 ; print join(',',@row), ".\n" ;
: splice(@row,0,4) ; print join(',',@row), ".\n" ;
: $#row += 4 ; print join(',',@row), ".\n" ;
:
: # prints out:
: # 1,2,3,4,5,6,7,8.
: # 1,2,3,4.
: # .
: # ,,,.
:
: # instead of:
: # 1,2,3,4,5,6,7,8.
: # 1,2,3,4.
: # .
: # 5,6,7,8.

Because splice decided to shorten the array from the top end, where
top end is defined by $#row, not by how much is actually allocated.
(In other words, splice is also allowed to play with the value of $#row.)
Had splice decided to shorten the array from the front, you would have
got the "undead" elements back. Note that if you only splice two elements
from the front instead of four, you'll get

1,2,3,4,5,6,7,8.
1,2,3,4.
3,4.
3,4,5,6,7,8.

: # Empty arrays can sometimes be (re-)extended, see fi:
:
: @row = 1..8 ; print join(',',@row), ".\n" ;
: $#row = -1 ; print join(',',@row), ".\n" ;
: $#row = 7 ; print join(',',@row), ".\n" ;
:
: # prints out:
: # 1,2,3,4,5,6,7,8.
: # .
: # 1,2,3,4,5,6,7,8.

The resurrection of "undead" strings is a quasi-feature that depends on the
laziness of perl in not deallocating the strings when you decrease $#foo.
I have yet to see a use for it, and am somewhat sorry I documented it.

With that in mind, I don't see any point in making it copy down undead
strings on the speculation that someone might increase $#foo again.
At least with the current setup, it makes an interesting detector of which
way splice decided to work.

: Can I see through this mistery with the knowledge of 'The Book' ?

It won't be covered in the book. The source code has to be useful for
something, after all... :-)

: I couldn't find it in the One True Man Page.

It's vaguely derivable from the documentation about splice growing
or shrinking the array, if you assume that the array that's growing or
shrinking consists only of the elements from $[ .. $#foo. (The fact
that splice is capable of shrinking arrays by upping the front pointer
as well as decreasing the length is not specifically mentioned anywhere
in the manual page, that I can recollect. Note that s/// does the same
tricks with strings when it decides it can modify a string in place. This
is part of the reason tokeners in 3.0 ran faster--the s/^keyword// construct
was no longer copying the line back and forth.)

Larry

Henk P. Penning

unread,
Oct 29, 1990, 4:23:34 PM10/29/90
to
# Why is it that:

@row = 1..8 ; print join(',',@row), ".\n" ;
$#row = 3 ; print join(',',@row), ".\n" ;
splice(@row,0,4) ; print join(',',@row), ".\n" ;
$#row += 4 ; print join(',',@row), ".\n" ;

# prints out:
# 1,2,3,4,5,6,7,8.
# 1,2,3,4.
# .
# ,,,.

# instead of:
# 1,2,3,4,5,6,7,8.
# 1,2,3,4.
# .
# 5,6,7,8.

# Empty arrays can sometimes be (re-)extended, see fi:

@row = 1..8 ; print join(',',@row), ".\n" ;
$#row = -1 ; print join(',',@row), ".\n" ;
$#row = 7 ; print join(',',@row), ".\n" ;

# prints out:
# 1,2,3,4,5,6,7,8.
# .
# 1,2,3,4,5,6,7,8.

__END__

Can I see through this mistery with the knowledge of 'The Book' ?

I couldn't find it in the One True Man Page.

=== HenkP ===
--
Henk P. Penning, Dept of Computer Science, Utrecht University.
Padualaan 14, P.O. Box 80.089, 3508 TB Utrecht, The Netherlands.
Telephone: +31-30-534106
e-mail : he...@cs.ruu.nl (uucp to hp4nl!ruuinf!henkp)

Reply all
Reply to author
Forward
0 new messages