develooper Front page | perl.perl5.porters | Postings from March 2006

[perl #38799] Problem in nested sorts

David McNerney
March 25, 2006 14:29
[perl #38799] Problem in nested sorts
Message ID:
# New Ticket Created by  David McNerney 
# Please include the string:  [perl #38799]
# in the subject line of all future correspondence about this issue. 
# <URL: >

This is a bug report for perl from,
generated with the help of perlbug 1.35 running under perl v5.8.6.

[Please enter your report here]

When an anonymous sort function invokes a method or function defined in another  package, and that 
method or function does multiple sorts using its own anonymous sort functions, the second and later
of these latter sorts fail; $a and $b are not defined. This does not occur if the function is moved
to the same package as the code performing the first sort, nor does it occur if the latter "nested"
sorts use a seperately defined sort function with prototypes (in which case, of course, the $a and $b
variables are not used). And, as implied above, the first "nested" sort executes correctly.

One person on usenet suggested that nobody ought to be doing such a thing as described above. Actually,
there are many times when working with a large object-oriented Perl library when one might need
to sort a list of objects, and compare values returned by object methods in the comparison function.
A programmer writing such code should not have to think about whether the object methods they choose
to invoke happen to use multiple non-prototyped sorts in their implementation.

Below is a test case, verified to demonstrate the problem on Perl 5.8.5 (Linux) and 5.8.6 (Mac OS X):

#!/usr/bin/perl -w
use strict;

package another;
#package my_package;
sub testFunction
	my @otherList = sort
		print "[testFunction, sort 1] a=$a b=$b\n";
		$a <=> $b
	} (5,4,3,2,1);
	print "\@otherList, sorted: ".join(", ", @otherList)."\n";

	my @otherList2 = sort
		print "[testFunction, sort 2] a=$a b=$b\n";
		$a <=> $b
	} (5,4,3,2,1);
	print "\@otherList2, sorted: ".join(", ", @otherList2)."\n";

package my_package;
my @list = (13,12,11);
@list = sort
	return $a <=> $b;
} @list;
print "\@list, sorted: ".join(", ", @list)."\n";


[Please do not change anything below this line]
Site configuration information for perl v5.8.6:

Configured by root at Sun Mar 20 16:31:42 PST 2005.

Summary of my perl5 (revision 5 version 8 subversion 6) configuration:
    osname=darwin, osvers=8.0, archname=darwin-thread-multi-2level
    uname='darwin 8.0 darwin kernel version 7.5.0: thu mar 3 18:48:46 pst 2005; root:xnuxnu-517.99.13.obj~1release_ppc power macintosh powerpc '
    config_args='-ds -e -Dprefix=/usr -Dccflags=-g  -pipe  -Dldflags=-Dman3ext=3pm -Duseithreads -Duseshrplib'
    hint=recommended, useposix=true, d_sigaction=define
    usethreads=define use5005threads=undef useithreads=define usemultiplicity=define
    useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
    use64bitint=undef use64bitall=undef uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
    cc='cc', ccflags ='-g -pipe -fno-common -DPERL_DARWIN -no-cpp-precomp -fno-strict-aliasing -I/usr/local/include',
    cppflags='-no-cpp-precomp -g -pipe -fno-common -DPERL_DARWIN -no-cpp-precomp -fno-strict-aliasing -I/usr/local/include'
    ccversion='', gccversion='3.3 20030304 (Apple Computer, Inc. build 1809)', gccosandvers=''
    intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=4321
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=8
    ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
    alignbytes=8, prototype=define
  Linker and Libraries:
    ld='env MACOSX_DEPLOYMENT_TARGET=10.3 cc', ldflags ='-L/usr/local/lib'
    libpth=/usr/local/lib /usr/lib
    libs=-ldbm -ldl -lm -lc
    perllibs=-ldl -lm -lc
    libc=/usr/lib/libc.dylib, so=dylib, useshrplib=true, libperl=libperl.dylib
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=bundle, d_dlsymun=undef, ccdlflags=' '
    cccdlflags=' ', lddlflags='-bundle -undefined dynamic_lookup -L/usr/local/lib'

Locally applied patches:
    23953 - fix for File::Path::rmtree CAN-2004-0452 security issue
    33990 - fix for setuid perl security issues

@INC for perl v5.8.6:

Environment for perl v5.8.6:
    LANG (unset)
    LANGUAGE (unset)
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PERL_BADLANG (unset)
    SHELL=/bin/bash Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About