From: Zefram Date: Tue, 13 Apr 2010 21:02:58 +0000 (+0100) Subject: don't use $[ in library code X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=859172fe8e35383381c4d2edab5f0525b3cb42e8;p=p5sagit%2Fp5-mst-13.2.git don't use $[ in library code Remove all uses of $[, both reads and writes, from library code. Test code (which must test behaviour of $[) is unchanged, as is the actual implementation of $[. Uses in CPAN libraries are also untouched: I've opened tickets at rt.cpan.org regarding them. --- diff --git a/lib/Dumpvalue.pm b/lib/Dumpvalue.pm index af16b1d..b5080a4 100644 --- a/lib/Dumpvalue.pm +++ b/lib/Dumpvalue.pm @@ -230,14 +230,14 @@ sub unwrap { if ($#$v >= 0) { $short = $sp . "0..$#{$v} " . join(" ", - map {exists $v->[$_] ? $self->stringify($v->[$_]) : "empty"} ($[..$tArrayDepth) + map {exists $v->[$_] ? $self->stringify($v->[$_]) : "empty"} (0..$tArrayDepth) ) . "$shortmore"; } else { $short = $sp . "empty array"; } (print "$short\n"), return if length $short <= $self->{compactDump}; } - for my $num ($[ .. $tArrayDepth) { + for my $num (0 .. $tArrayDepth) { return if $DB::signal and $self->{stopDbSignal}; print "$sp$num "; if (exists $v->[$num]) { diff --git a/lib/abbrev.pl b/lib/abbrev.pl index cd20063..673783e 100644 --- a/lib/abbrev.pl +++ b/lib/abbrev.pl @@ -22,7 +22,6 @@ sub main'abbrev { local(*domain) = @_; shift(@_); @cmp = @_; - local($[) = 0; foreach $name (@_) { @extra = split(//,$name); $abbrev = shift(@extra); diff --git a/lib/assert.pl b/lib/assert.pl index 2c2073c..10d2d1a 100644 --- a/lib/assert.pl +++ b/lib/assert.pl @@ -18,7 +18,7 @@ # routine shamelessly borrowed from the perl debugger. sub assert { - &panic("ASSERTION BOTCHED: $_[$[]",$@) unless eval $_[$[]; + &panic("ASSERTION BOTCHED: $_[0]",$@) unless eval $_[0]; } sub panic { diff --git a/lib/bigfloat.pl b/lib/bigfloat.pl index dd0bc05..b526b8e 100644 --- a/lib/bigfloat.pl +++ b/lib/bigfloat.pl @@ -79,7 +79,7 @@ sub norm { #(mantissa, exponent) return fnum_str # negation sub main'fneg { #(fnum_str) return fnum_str - local($_) = &'fnorm($_[$[]); + local($_) = &'fnorm($_[0]); vec($_,0,8) ^= ord('+') ^ ord('-') unless $_ eq '+0E+0'; # flip sign if ( ord("\t") == 9 ) { # ascii s/^H/N/; @@ -92,14 +92,14 @@ sub main'fneg { #(fnum_str) return fnum_str # absolute value sub main'fabs { #(fnum_str) return fnum_str - local($_) = &'fnorm($_[$[]); + local($_) = &'fnorm($_[0]); s/^-/+/; # mash sign $_; } # multiplication sub main'fmul { #(fnum_str, fnum_str) return fnum_str - local($x,$y) = (&'fnorm($_[$[]),&'fnorm($_[$[+1])); + local($x,$y) = (&'fnorm($_[0]),&'fnorm($_[1])); if ($x eq 'NaN' || $y eq 'NaN') { 'NaN'; } else { @@ -111,7 +111,7 @@ sub main'fmul { #(fnum_str, fnum_str) return fnum_str # addition sub main'fadd { #(fnum_str, fnum_str) return fnum_str - local($x,$y) = (&'fnorm($_[$[]),&'fnorm($_[$[+1])); + local($x,$y) = (&'fnorm($_[0]),&'fnorm($_[1])); if ($x eq 'NaN' || $y eq 'NaN') { 'NaN'; } else { @@ -124,7 +124,7 @@ sub main'fadd { #(fnum_str, fnum_str) return fnum_str # subtraction sub main'fsub { #(fnum_str, fnum_str) return fnum_str - &'fadd($_[$[],&'fneg($_[$[+1])); + &'fadd($_[0],&'fneg($_[1])); } # division @@ -132,7 +132,7 @@ sub main'fsub { #(fnum_str, fnum_str) return fnum_str # result has at most max(scale, length(dividend), length(divisor)) digits sub main'fdiv #(fnum_str, fnum_str[,scale]) return fnum_str { - local($x,$y,$scale) = (&'fnorm($_[$[]),&'fnorm($_[$[+1]),$_[$[+2]); + local($x,$y,$scale) = (&'fnorm($_[0]),&'fnorm($_[1]),$_[2]); if ($x eq 'NaN' || $y eq 'NaN' || $y eq '+0E+0') { 'NaN'; } else { @@ -159,13 +159,13 @@ sub round { #(int_str, int_str, int_str) return int_str if ( $cmp < 0 || ($cmp == 0 && ( $rnd_mode eq 'zero' || - ($rnd_mode eq '-inf' && (substr($q,$[,1) eq '+')) || - ($rnd_mode eq '+inf' && (substr($q,$[,1) eq '-')) || + ($rnd_mode eq '-inf' && (substr($q,0,1) eq '+')) || + ($rnd_mode eq '+inf' && (substr($q,0,1) eq '-')) || ($rnd_mode eq 'even' && $q =~ /[24680]$/) || ($rnd_mode eq 'odd' && $q =~ /[13579]$/) )) ) { $q; # round down } else { - &'badd($q, ((substr($q,$[,1) eq '-') ? '-1' : '+1')); + &'badd($q, ((substr($q,0,1) eq '-') ? '-1' : '+1')); # round up } } @@ -173,7 +173,7 @@ sub round { #(int_str, int_str, int_str) return int_str # round the mantissa of $x to $scale digits sub main'fround { #(fnum_str, scale) return fnum_str - local($x,$scale) = (&'fnorm($_[$[]),$_[$[+1]); + local($x,$scale) = (&'fnorm($_[0]),$_[1]); if ($x eq 'NaN' || $scale <= 0) { $x; } else { @@ -181,8 +181,8 @@ sub main'fround { #(fnum_str, scale) return fnum_str if (length($xm)-1 <= $scale) { $x; } else { - &norm(&round(substr($xm,$[,$scale+1), - "+0".substr($xm,$[+$scale+1,1),"+10"), + &norm(&round(substr($xm,0,$scale+1), + "+0".substr($xm,$scale+1,1),"+10"), $xe+length($xm)-$scale-1); } } @@ -190,7 +190,7 @@ sub main'fround { #(fnum_str, scale) return fnum_str # round $x at the 10 to the $scale digit place sub main'ffround { #(fnum_str, scale) return fnum_str - local($x,$scale) = (&'fnorm($_[$[]),$_[$[+1]); + local($x,$scale) = (&'fnorm($_[0]),$_[1]); if ($x eq 'NaN') { 'NaN'; } else { @@ -206,11 +206,11 @@ sub main'ffround { #(fnum_str, scale) return fnum_str # we'll pass a non-normalized "-0" to &round when rounding # -0.006 (for example), purely so that &round won't lose # the sign. - &norm(&round(substr($xm,$[,1).'0', - "+0".substr($xm,$[+1,1),"+10"), $scale); + &norm(&round(substr($xm,0,1).'0', + "+0".substr($xm,1,1),"+10"), $scale); } else { - &norm(&round(substr($xm,$[,$xe), - "+0".substr($xm,$[+$xe,1),"+10"), $scale); + &norm(&round(substr($xm,0,$xe), + "+0".substr($xm,$xe,1),"+10"), $scale); } } } @@ -220,14 +220,14 @@ sub main'ffround { #(fnum_str, scale) return fnum_str # returns undef if either or both input value are not numbers sub main'fcmp #(fnum_str, fnum_str) return cond_code { - local($x, $y) = (&'fnorm($_[$[]),&'fnorm($_[$[+1])); + local($x, $y) = (&'fnorm($_[0]),&'fnorm($_[1])); if ($x eq "NaN" || $y eq "NaN") { undef; } else { ord($y) <=> ord($x) || ( local($xm,$xe,$ym,$ye) = split('E', $x."E$y"), - (($xe <=> $ye) * (substr($x,$[,1).'1') + (($xe <=> $ye) * (substr($x,0,1).'1') || &bigint'cmp($xm,$ym)) ); } @@ -235,7 +235,7 @@ sub main'fcmp #(fnum_str, fnum_str) return cond_code # square root by Newtons method. sub main'fsqrt { #(fnum_str[, scale]) return fnum_str - local($x, $scale) = (&'fnorm($_[$[]), $_[$[+1]); + local($x, $scale) = (&'fnorm($_[0]), $_[1]); if ($x eq 'NaN' || $x =~ /^-/) { 'NaN'; } elsif ($x eq '+0E+0') { diff --git a/lib/bigint.pl b/lib/bigint.pl index d28f0e2..941d53d 100644 --- a/lib/bigint.pl +++ b/lib/bigint.pl @@ -61,7 +61,7 @@ sub main'bnorm { #(num_str) return num_str local($_) = @_; s/\s+//g; # strip white space if (s/^([+-]?)0*(\d+)$/$1$2/) { # test if number - substr($_,$[,0) = '+' unless $1; # Add missing sign + substr($_,0,0) = '+' unless $1; # Add missing sign s/^-0/+0/; $_; } else { @@ -73,8 +73,8 @@ sub main'bnorm { #(num_str) return num_str # Assumes normalized value as input. sub internal { #(num_str) return int_num_array local($d) = @_; - ($is,$il) = (substr($d,$[,1),length($d)-2); - substr($d,$[,1) = ''; + ($is,$il) = (substr($d,0,1),length($d)-2); + substr($d,0,1) = ''; ($is, reverse(unpack("a" . ($il%5+1) . ("a5" x ($il/5)), $d))); } @@ -107,7 +107,7 @@ sub abs { # post-normalized abs for internal use # Compares 2 values. Returns one of undef, <0, =0, >0. (suitable for sort) sub main'bcmp { #(num_str, num_str) return cond_code - local($x,$y) = (&'bnorm($_[$[]),&'bnorm($_[$[+1])); + local($x,$y) = (&'bnorm($_[0]),&'bnorm($_[1])); if ($x eq 'NaN') { undef; } elsif ($y eq 'NaN') { @@ -139,7 +139,7 @@ sub cmp { # post-normalized compare for internal use } sub main'badd { #(num_str, num_str) return num_str - local(*x, *y); ($x, $y) = (&'bnorm($_[$[]),&'bnorm($_[$[+1])); + local(*x, *y); ($x, $y) = (&'bnorm($_[0]),&'bnorm($_[1])); if ($x eq 'NaN') { 'NaN'; } elsif ($y eq 'NaN') { @@ -162,12 +162,12 @@ sub main'badd { #(num_str, num_str) return num_str } sub main'bsub { #(num_str, num_str) return num_str - &'badd($_[$[],&'bneg($_[$[+1])); + &'badd($_[0],&'bneg($_[1])); } # GCD -- Euclids algorithm Knuth Vol 2 pg 296 sub main'bgcd { #(num_str, num_str) return num_str - local($x,$y) = (&'bnorm($_[$[]),&'bnorm($_[$[+1])); + local($x,$y) = (&'bnorm($_[0]),&'bnorm($_[1])); if ($x eq 'NaN' || $y eq 'NaN') { 'NaN'; } else { @@ -206,7 +206,7 @@ sub sub { #(int_num_array, int_num_array) return int_num_array # multiply two numbers -- stolen from Knuth Vol 2 pg 233 sub main'bmul { #(num_str, num_str) return num_str - local(*x, *y); ($x, $y) = (&'bnorm($_[$[]), &'bnorm($_[$[+1])); + local(*x, *y); ($x, $y) = (&'bnorm($_[0]), &'bnorm($_[1])); if ($x eq 'NaN') { 'NaN'; } elsif ($y eq 'NaN') { @@ -217,7 +217,7 @@ sub main'bmul { #(num_str, num_str) return num_str local($signr) = (shift @x ne shift @y) ? '-' : '+'; @prod = (); for $x (@x) { - ($car, $cty) = (0, $[); + ($car, $cty) = (0, 0); for $y (@y) { $prod = $x * $y + $prod[$cty] + $car; if ($use_mult) { @@ -238,16 +238,16 @@ sub main'bmul { #(num_str, num_str) return num_str # modulus sub main'bmod { #(num_str, num_str) return num_str - (&'bdiv(@_))[$[+1]; + (&'bdiv(@_))[1]; } sub main'bdiv { #(dividend: num_str, divisor: num_str) return num_str - local (*x, *y); ($x, $y) = (&'bnorm($_[$[]), &'bnorm($_[$[+1])); + local (*x, *y); ($x, $y) = (&'bnorm($_[0]), &'bnorm($_[1])); return wantarray ? ('NaN','NaN') : 'NaN' if ($x eq 'NaN' || $y eq 'NaN' || $y eq '+0'); return wantarray ? ('+0',$x) : '+0' if (&cmp(&abs($x),&abs($y)) < 0); @x = &internal($x); @y = &internal($y); - $srem = $y[$[]; + $srem = $y[0]; $sr = (shift @x ne shift @y) ? '-' : '+'; $car = $bar = $prd = 0; if (($dd = int(1e5/($y[$#y]+1))) != 1) { @@ -281,7 +281,7 @@ sub main'bdiv { #(dividend: num_str, divisor: num_str) return num_str --$q while ($v2*$q > ($u0*1e5+$u1-$q*$v1)*1e5+$u2); if ($q) { ($car, $bar) = (0,0); - for ($y = $[, $x = $#x-$#y+$[-1; $y <= $#y; ++$y,++$x) { + for ($y = 0, $x = $#x-$#y-1; $y <= $#y; ++$y,++$x) { $prd = $q * $y[$y] + $car; if ($use_mult) { $prd -= ($car = int($prd * 1e-5)) * 1e5; @@ -293,7 +293,7 @@ sub main'bdiv { #(dividend: num_str, divisor: num_str) return num_str } if ($x[$#x] < $car + $bar) { $car = 0; --$q; - for ($y = $[, $x = $#x-$#y+$[-1; $y <= $#y; ++$y,++$x) { + for ($y = 0, $x = $#x-$#y-1; $y <= $#y; ++$y,++$x) { $x[$x] -= 1e5 if ($car = (($x[$x] += $y[$y] + $car) > 1e5)); } diff --git a/lib/bigrat.pl b/lib/bigrat.pl index f3b5995..6f5905f 100644 --- a/lib/bigrat.pl +++ b/lib/bigrat.pl @@ -71,7 +71,7 @@ sub norm { #(bint, bint) return rat_num $num = &'bnorm($num); $dom = &'bnorm($dom); } - substr($dom,$[,1) = ''; + substr($dom,0,1) = ''; "$num/$dom"; } } @@ -86,42 +86,42 @@ sub main'rneg { #(rat_num) return rat_num # absolute value sub main'rabs { #(rat_num) return $rat_num local($_) = &'rnorm(@_); - substr($_,$[,1) = '+' unless $_ eq 'NaN'; + substr($_,0,1) = '+' unless $_ eq 'NaN'; $_; } # multipication sub main'rmul { #(rat_num, rat_num) return rat_num - local($xn,$xd) = split('/',&'rnorm($_[$[])); - local($yn,$yd) = split('/',&'rnorm($_[$[+1])); + local($xn,$xd) = split('/',&'rnorm($_[0])); + local($yn,$yd) = split('/',&'rnorm($_[1])); &norm(&'bmul($xn,$yn),&'bmul($xd,$yd)); } # division sub main'rdiv { #(rat_num, rat_num) return rat_num - local($xn,$xd) = split('/',&'rnorm($_[$[])); - local($yn,$yd) = split('/',&'rnorm($_[$[+1])); + local($xn,$xd) = split('/',&'rnorm($_[0])); + local($yn,$yd) = split('/',&'rnorm($_[1])); &norm(&'bmul($xn,$yd),&'bmul($xd,$yn)); } # addition sub main'radd { #(rat_num, rat_num) return rat_num - local($xn,$xd) = split('/',&'rnorm($_[$[])); - local($yn,$yd) = split('/',&'rnorm($_[$[+1])); + local($xn,$xd) = split('/',&'rnorm($_[0])); + local($yn,$yd) = split('/',&'rnorm($_[1])); &norm(&'badd(&'bmul($xn,$yd),&'bmul($yn,$xd)),&'bmul($xd,$yd)); } # subtraction sub main'rsub { #(rat_num, rat_num) return rat_num - local($xn,$xd) = split('/',&'rnorm($_[$[])); - local($yn,$yd) = split('/',&'rnorm($_[$[+1])); + local($xn,$xd) = split('/',&'rnorm($_[0])); + local($yn,$yd) = split('/',&'rnorm($_[1])); &norm(&'bsub(&'bmul($xn,$yd),&'bmul($yn,$xd)),&'bmul($xd,$yd)); } # comparison sub main'rcmp { #(rat_num, rat_num) return cond_code - local($xn,$xd) = split('/',&'rnorm($_[$[])); - local($yn,$yd) = split('/',&'rnorm($_[$[+1])); + local($xn,$xd) = split('/',&'rnorm($_[0])); + local($yn,$yd) = split('/',&'rnorm($_[1])); &bigint'cmp(&'bmul($xn,$yd),&'bmul($yn,$xd)); } @@ -139,7 +139,7 @@ sub main'rmod { #(rat_num) return (rat_num,rat_num) # square root by Newtons method. # cycles specifies the number of iterations default: 5 sub main'rsqrt { #(fnum_str[, cycles]) return fnum_str - local($x, $scale) = (&'rnorm($_[$[]), $_[$[+1]); + local($x, $scale) = (&'rnorm($_[0]), $_[1]); if ($x eq 'NaN') { 'NaN'; } elsif ($x =~ /^-/) { diff --git a/lib/charnames.pm b/lib/charnames.pm index 62a057f..2bf36ac 100644 --- a/lib/charnames.pm +++ b/lib/charnames.pm @@ -304,13 +304,13 @@ sub vianame $txt = do "unicore/Name.pl" unless $txt; my $pos = index $txt, "\t\t$arg\n"; - if ($[ <= $pos) { + if (0 <= $pos) { my $posLF = rindex $txt, "\n", $pos; (my $code = substr $txt, $posLF + 1, 6) =~ tr/\t//d; return $vianame{$arg} = CORE::hex $code; - # If $pos is at the 1st line, $posLF must be $[ - 1 (not found); - # then $posLF + 1 equals to $[ (at the beginning of $txt). + # If $pos is at the 1st line, $posLF must be -1 (not found); + # then $posLF + 1 equals to 0 (at the beginning of $txt). # Otherwise $posLF is the position of "\n"; # then $posLF + 1 must be the position of the next to "\n" # (the beginning of the line). diff --git a/lib/ctime.pl b/lib/ctime.pl index 1b91ec2..1db90c4 100644 --- a/lib/ctime.pl +++ b/lib/ctime.pl @@ -35,7 +35,6 @@ sub ctime { package ctime; local($time) = @_; - local($[) = 0; local($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst); # Determine what time zone is in effect. diff --git a/lib/dumpvar.pl b/lib/dumpvar.pl index 0268cea..523bda8 100644 --- a/lib/dumpvar.pl +++ b/lib/dumpvar.pl @@ -251,7 +251,7 @@ sub unwrap { if ($#$v >= 0) { $short = $sp . "0..$#{$v} " . join(" ", - map {exists $v->[$_] ? stringify $v->[$_] : "empty"} ($[..$tArrayDepth) + map {exists $v->[$_] ? stringify $v->[$_] : "empty"} (0..$tArrayDepth) ) . "$shortmore"; } else { $short = $sp . "empty array"; @@ -262,7 +262,7 @@ sub unwrap { # print "$short\n"; # return; #} - for $num ($[ .. $tArrayDepth) { + for $num (0 .. $tArrayDepth) { return if $DB::signal; print "$sp$num "; if (exists $v->[$num]) { diff --git a/lib/getcwd.pl b/lib/getcwd.pl index fcca2ce..1c6b761 100644 --- a/lib/getcwd.pl +++ b/lib/getcwd.pl @@ -38,7 +38,7 @@ sub getcwd closedir(getcwd'PARENT); #'); return ''; } - if ($pst[$[] == $cst[$[] && $pst[$[ + 1] == $cst[$[ + 1]) + if ($pst[0] == $cst[0] && $pst[1] == $cst[1]) { $dir = ''; } @@ -59,8 +59,8 @@ sub getcwd # return ''; } } - while ($dir eq '.' || $dir eq '..' || $tst[$[] != $pst[$[] || - $tst[$[ + 1] != $pst[$[ + 1]); + while ($dir eq '.' || $dir eq '..' || $tst[0] != $pst[0] || + $tst[1] != $pst[1]); } $cwd = "$dir/$cwd"; closedir(getcwd'PARENT); #'); diff --git a/lib/getopt.pl b/lib/getopt.pl index 343921f..2c05b2b 100644 --- a/lib/getopt.pl +++ b/lib/getopt.pl @@ -22,11 +22,10 @@ sub Getopt { local($argumentative) = @_; local($_,$first,$rest); - local($[) = 0; while (@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) { ($first,$rest) = ($1,$2); - if (index($argumentative,$first) >= $[) { + if (index($argumentative,$first) >= 0) { if ($rest ne '') { shift(@ARGV); } diff --git a/lib/getopts.pl b/lib/getopts.pl index b2aa5cb..928bb22 100644 --- a/lib/getopts.pl +++ b/lib/getopts.pl @@ -16,13 +16,12 @@ sub Getopts { local($argumentative) = @_; local(@args,$_,$first,$rest); local($errs) = 0; - local($[) = 0; @args = split( / */, $argumentative ); while(@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) { ($first,$rest) = ($1,$2); $pos = index($argumentative,$first); - if($pos >= $[) { + if($pos >= 0) { if($args[$pos+1] eq ':') { shift(@ARGV); if($rest eq '') { diff --git a/lib/stat.pl b/lib/stat.pl index 4174d60..d73e410 100644 --- a/lib/stat.pl +++ b/lib/stat.pl @@ -7,19 +7,19 @@ ;# $st_dev = @ary[$ST_DEV]; ;# -$ST_DEV = 0 + $[; -$ST_INO = 1 + $[; -$ST_MODE = 2 + $[; -$ST_NLINK = 3 + $[; -$ST_UID = 4 + $[; -$ST_GID = 5 + $[; -$ST_RDEV = 6 + $[; -$ST_SIZE = 7 + $[; -$ST_ATIME = 8 + $[; -$ST_MTIME = 9 + $[; -$ST_CTIME = 10 + $[; -$ST_BLKSIZE = 11 + $[; -$ST_BLOCKS = 12 + $[; +$ST_DEV = 0; +$ST_INO = 1; +$ST_MODE = 2; +$ST_NLINK = 3; +$ST_UID = 4; +$ST_GID = 5; +$ST_RDEV = 6; +$ST_SIZE = 7; +$ST_ATIME = 8; +$ST_MTIME = 9; +$ST_CTIME = 10; +$ST_BLKSIZE = 11; +$ST_BLOCKS = 12; ;# Usage: ;# require 'stat.pl'; diff --git a/lib/termcap.pl b/lib/termcap.pl index 247a171..4c029d3 100644 --- a/lib/termcap.pl +++ b/lib/termcap.pl @@ -155,8 +155,8 @@ sub Tgoto { } elsif ($code eq '>') { ($code,$tmp,$string) = unpack("CCa99",$string); - if ($tmp[$[] > $code) { - $tmp[$[] += $tmp; + if ($tmp[0] > $code) { + $tmp[0] += $tmp; } } elsif ($code eq '2') { diff --git a/pod/perlembed.pod b/pod/perlembed.pod index 5ecaed0..488fe1b 100644 --- a/pod/perlembed.pod +++ b/pod/perlembed.pod @@ -490,7 +490,7 @@ been wrapped here): SvREFCNT_dec(command); *match_list = get_av("array", 0); - num_matches = av_len(*match_list) + 1; /** assume $[ is 0 **/ + num_matches = av_len(*match_list) + 1; return num_matches; }