Add a regression test for %^H.
Change the sort pragma implementation to use a
global variable instead of %^H.
p4raw-id: //depot/perl@16286
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
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;
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 '$_'");
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);
# 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{
#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)
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;
{
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
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;
}
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:
--- /dev/null
+#!./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";
+}