summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRafael Garcia-Suarez <rgarciasuarez@gmail.com>2002-04-30 19:03:34 +0000
committerRafael Garcia-Suarez <rgarciasuarez@gmail.com>2002-04-30 19:03:34 +0000
commit045ac3170cd33a7002e5d26e4859e94ca400926d (patch)
treebf481b2a683c7ccbf658d60a50623597b094923e
parente8c86ba6ca66f86dc4c8f4de0abf70f53c2484f4 (diff)
downloadperl-045ac3170cd33a7002e5d26e4859e94ca400926d.tar.gz
Fix bug id 20020427.004 on %^H.
Add a regression test for %^H. Change the sort pragma implementation to use a global variable instead of %^H. p4raw-id: //depot/perl@16286
-rw-r--r--MANIFEST1
-rw-r--r--lib/sort.pm30
-rw-r--r--lib/sort.t5
-rw-r--r--pp_sort.c15
-rw-r--r--scope.c4
-rw-r--r--t/comp/hints.t36
6 files changed, 67 insertions, 24 deletions
diff --git a/MANIFEST b/MANIFEST
index 1af1242183..f474a1d415 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -2291,6 +2291,7 @@ t/comp/colon.t See if colons are parsed correctly
t/comp/cpp.aux main file for cpp.t
t/comp/cpp.t See if C preprocessor works
t/comp/decl.t See if declarations work
+t/comp/hints.t See if %^H works
t/comp/multiline.t See if multiline strings work
t/comp/package.t See if packages work
t/comp/proto.t See if function prototypes work
diff --git a/lib/sort.pm b/lib/sort.pm
index 27efbf5904..3db4777c05 100644
--- a/lib/sort.pm
+++ b/lib/sort.pm
@@ -1,8 +1,13 @@
package sort;
-our $VERSION = '1.00';
+our $VERSION = '1.01';
-$sort::hint_bits = 0x00020000; # HINT_LOCALIZE_HH, really...
+# Currently the hints for pp_sort are stored in the global variable
+# $sort::hints. An improvement would be to store them in $^H{SORT} and have
+# this information available somewhere in the listop OP_SORT, to allow lexical
+# scoping of this pragma. -- rgs 2002-04-30
+
+our $hints = 0;
$sort::quicksort_bit = 0x00000001;
$sort::mergesort_bit = 0x00000002;
@@ -17,18 +22,17 @@ sub import {
require Carp;
Carp::croak("sort pragma requires arguments");
}
- $^H |= $sort::hint_bits;
local $_;
- no warnings 'uninitialized'; # $^H{SORT} bitops would warn
+ no warnings 'uninitialized'; # bitops would warn
while ($_ = shift(@_)) {
if (/^_q(?:uick)?sort$/) {
- $^H{SORT} &= ~$sort::sort_bits;
- $^H{SORT} |= $sort::quicksort_bit;
+ $hints &= ~$sort::sort_bits;
+ $hints |= $sort::quicksort_bit;
} elsif ($_ eq '_mergesort') {
- $^H{SORT} &= ~$sort::sort_bits;
- $^H{SORT} |= $sort::mergesort_bit;
+ $hints &= ~$sort::sort_bits;
+ $hints |= $sort::mergesort_bit;
} elsif ($_ eq 'stable') {
- $^H{SORT} |= $sort::stable_bit;
+ $hints |= $sort::stable_bit;
} else {
require Carp;
Carp::croak("sort: unknown subpragma '$_'");
@@ -38,10 +42,10 @@ sub import {
sub current {
my @sort;
- if ($^H{SORT}) {
- push @sort, 'quicksort' if $^H{SORT} & $sort::quicksort_bit;
- push @sort, 'mergesort' if $^H{SORT} & $sort::mergesort_bit;
- push @sort, 'stable' if $^H{SORT} & $sort::stable_bit;
+ if ($hints) {
+ push @sort, 'quicksort' if $hints & $sort::quicksort_bit;
+ push @sort, 'mergesort' if $hints & $sort::mergesort_bit;
+ push @sort, 'stable' if $hints & $sort::stable_bit;
}
push @sort, 'mergesort' unless @sort;
join(' ', @sort);
diff --git a/lib/sort.t b/lib/sort.t
index fbeaacfb5f..990376576b 100644
--- a/lib/sort.t
+++ b/lib/sort.t
@@ -136,9 +136,8 @@ main(0);
# XXX We're using this eval "..." trick to force recompilation,
# to ensure that the correct pragma is enabled when main() is run.
-# Currently 'use sort' modifies $^H{SORT} at compile-time, but
-# pp_sort() fetches its value at run-time : thus the lexical scoping
-# of %^H is of no utility.
+# Currently 'use sort' modifies $sort::hints at compile-time, but
+# pp_sort() fetches its value at run-time.
# The order of those evals is important.
eval q{
diff --git a/pp_sort.c b/pp_sort.c
index 5d6ce8698e..0a50ed53bb 100644
--- a/pp_sort.c
+++ b/pp_sort.c
@@ -34,10 +34,9 @@ static I32 amagic_cmp_locale(pTHX_ SV *a, SV *b);
#define sv_cmp_static Perl_sv_cmp
#define sv_cmp_locale_static Perl_sv_cmp_locale
-#define SORTHINTS(hintsvp) \
- ((PL_hintgv && \
- (hintsvp = hv_fetch(GvHV(PL_hintgv), "SORT", 4, FALSE))) ? \
- (I32)SvIV(*hintsvp) : 0)
+#define SORTHINTS(hintsv) \
+ (((hintsv) = GvSV(gv_fetchpv("sort::hints", GV_ADDMULTI, SVt_IV))), \
+ (SvIOK(hintsv) ? ((I32)SvIV(hintsv)) : 0))
#ifndef SMALLSORT
#define SMALLSORT (200)
@@ -1304,9 +1303,9 @@ cmpindir(pTHX_ gptr a, gptr b)
STATIC void
S_qsortsv(pTHX_ gptr *list1, size_t nmemb, SVCOMPARE_t cmp)
{
- SV **hintsvp;
+ SV *hintsv;
- if (SORTHINTS(hintsvp) & HINT_SORT_STABLE) {
+ if (SORTHINTS(hintsv) & HINT_SORT_STABLE) {
register gptr **pp, *q;
register size_t n, j, i;
gptr *small[SMALLSORT], **indir, tmp;
@@ -1391,7 +1390,7 @@ Perl_sortsv(pTHX_ SV **array, size_t nmemb, SVCOMPARE_t cmp)
{
void (*sortsvp)(pTHX_ SV **array, size_t nmemb, SVCOMPARE_t cmp) =
S_mergesortsv;
- SV **hintsvp;
+ SV *hintsv;
I32 hints;
/* Sun's Compiler (cc: WorkShop Compilers 4.2 30 Oct 1996 C 4.2) used
@@ -1399,7 +1398,7 @@ Perl_sortsv(pTHX_ SV **array, size_t nmemb, SVCOMPARE_t cmp)
errors related to picking the correct sort() function, try recompiling
this file without optimiziation. -- A.D. 4/2002.
*/
- hints = SORTHINTS(hintsvp);
+ hints = SORTHINTS(hintsv);
if (hints & HINT_SORT_QUICKSORT) {
sortsvp = S_qsortsv;
}
diff --git a/scope.c b/scope.c
index 595fe12665..85a068017c 100644
--- a/scope.c
+++ b/scope.c
@@ -959,6 +959,10 @@ Perl_leave_scope(pTHX_ I32 base)
PL_op = (OP*)SSPOPPTR;
break;
case SAVEt_HINTS:
+ if ((PL_hints & HINT_LOCALIZE_HH) && GvHV(PL_hintgv)) {
+ SvREFCNT_dec((SV*)GvHV(PL_hintgv));
+ GvHV(PL_hintgv) = NULL;
+ }
*(I32*)&PL_hints = (I32)SSPOPINT;
break;
case SAVEt_COMPPAD:
diff --git a/t/comp/hints.t b/t/comp/hints.t
new file mode 100644
index 0000000000..5911b77688
--- /dev/null
+++ b/t/comp/hints.t
@@ -0,0 +1,36 @@
+#!./perl -w
+
+BEGIN { print "1..7\n"; }
+BEGIN {
+ print "not " if exists $^H{foo};
+ print "ok 1 - \$^H{foo} doesn't exist initially\n";
+}
+{
+ # simulate a pragma -- don't forget HINT_LOCALIZE_HH
+ BEGIN { $^H |= 0x00020000; $^H{foo} = "a"; }
+ BEGIN {
+ print "not " if $^H{foo} ne "a";
+ print "ok 2 - \$^H{foo} is now 'a'\n";
+ }
+ {
+ BEGIN { $^H |= 0x00020000; $^H{foo} = "b"; }
+ BEGIN {
+ print "not " if $^H{foo} ne "b";
+ print "ok 3 - \$^H{foo} is now 'b'\n";
+ }
+ }
+ BEGIN {
+ print "not " if $^H{foo} ne "a";
+ print "ok 4 - \$H^{foo} restored to 'a'\n";
+ }
+ CHECK {
+ print "not " if exists $^H{foo};
+ print "ok 6 - \$^H{foo} doesn't exist when compilation complete\n";
+ }
+ print "not " if exists $^H{foo};
+ print "ok 7 - \$^H{foo} doesn't exist at runtime\n";
+}
+BEGIN {
+ print "not " if exists $^H{foo};
+ print "ok 5 - \$^H{foo} doesn't exist while finishing compilation\n";
+}