[comp.lang.perl] perl-4.010 bug on subroutine calls

rich@StarConn.com (Richard Mahn) (06/27/91)

There seems to be a problem with conflicts in the names of variables in
a call to a program.  If the name of any variable in the calling routine is
declared as local in a subroutine, 1) call-by-reference seems to be
disabled, and 2) @_ seems to be "bound" to declared local variables.
The following routine gives:

#!/usr/local/bin/perl
$var="old";    &S1($var);    print "first test: $var   ";
$var1="old";   &S1($var1);   print "second test: $var1\n";
sub S1 {local($var); $_[0]="new";}

Or, more interesting and potentially destructive:

#!/usr/local/bin/perl
$var="old";    &S2($var);
$var2="old";   &S2($var2);
sub S2 {local($var);$var="new";print "$_[0]\n";}

which prints:
new
old

I am using:
$RCSfile: perl.c,v $$Revision: 4.0.1.4 $$Date: 91/06/10 01:23:07 $
Patch level: 10
on Sun Sparcstation, SunOS 4.1.  I have also tried this on SunOS 4.1.1 and
SunOS 3.5 and saw the same problems.  Likewise with pl 4.003 and pl3.044
on SunOS 4.1

----
	Rich Mahn			rich@StarConn.com
	Starnet Connections		rich@starnet.uucp

flee@cs.psu.edu (Felix Lee) (06/28/91)

Richard Mann reports that
	$x = "black"; &bleach($x); print $x;
	sub bleach { local($x); $_[0] = "white"; }
prints "black" rather than "white".

This is the opposite of the old "variable suicide" problem.  Before
perl 3.042, the following
	$x = "black"; &print("white", $x);
	sub print { local($x, $y) = @_; print "$x $y"; }
would print "white white" rather than "white black".
--
Felix Lee	flee@cs.psu.edu