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

[perl #40957] Re: Date::Parse mysteriously lowercases text when an unrelated variable is used.

Thread Next
From:
perlbug-followup
Date:
November 21, 2006 00:26
Subject:
[perl #40957] Re: Date::Parse mysteriously lowercases text when an unrelated variable is used.
Message ID:
rt-3.5.HEAD-32138-1164060561-175.40957-75-0@perl.org
# New Ticket Created by  "Mumia W. (reading news)" 
# Please include the string:  [perl #40957]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=40957 >


On 11/19/2006 08:48 AM, Peter Scott wrote:
> On Sun, 19 Nov 2006 12:35:14 +0000, Mumia W. (reading news) wrote:
>> To reduce my boredom I took grocery_stocker's data from "At a loss how 
>> to sort this file," and I wrote a program to sort it:
>>
>>         1 #!/usr/bin/perl
>>         2
>>         3 use strict;
>>         4 use warnings;
>>         5 use Date::Parse;
>>         6 my $reverser = -1;
>>         7
>>         8 my $sortfn = sub {
>>         9     my ($c, $d) = map str2time(substr($_, 43, 16)), ($a, $b);
>>        10     $reverser * ($c <=> $d);
>>        11 };
>>        12
>>        13 @ARGV = 'sort-long-dates.txt';
>>        14 my @data = map uc $_, grep /./, <>;
>>        15 my @sorted = sort $sortfn @data;
>>        16 print join("",@sorted), "\n";
>>        17
>>
>> The data is sorted as expected, but something strange happens: 
>> Date::Parse::str2time lowercases the weekday names. I deliberately 
>> capitalize everything on the line to show that only the dates are affected.
> [snip]
>> BTW, the dates are not modified if the $reverser variable is defined 
>> within the $sortfn subroutine, e.g.
>>
>>         7 my $sortfn = sub {
>>         8     my $reverser = -1;
>>         9     my ($c, $d) = map str2time(substr($_, 43, 16)), ($a, $b);
>>        10     $reverser * ($c <=> $d);
>>        11 };
>>
>> What's going on?
>>
>> -------------------------------
>> Perl 5.8.4
>> Debian GNU/Linux 3.1
>> Date::Parse 2.27
> 
> Good report.  I suspect you are running into the bug reported at
> http://groups-beta.google.com/group/perl.perl5.porters/browse_thread/thread/eefb0a6227a31891/7b14af5525b9d861?hl=en
> ..  Maybe you could try 5.9.3 on your program and let us know.  If it's not
> fixed you can dust off perlbug.
> 
> str2time does sort() and lc() internally.  Apparently the bug has
> something to do with sort subs that are closures (moving the definition of
> $reverser out of the sub does that).
> 

Indeed, that looks like the bug, and it looks like it's still in Perl 5.9.4:

--------PROGRAM FOLLOWS---------

#!/usr/local/bin/perl5.9.4

use strict;
use warnings;
use Date::Parse;
my $reverser = -1;

printf ("Perl version: %vd\n", $^V);
printf ("Date::Parse version: $Date::Parse::VERSION\n");
# print '-------------------', "\n";

# Use a sorting function that is NOT a closure.
my $sortfn1 = sub {
     my ($c, $d) = map str2time(substr($_, 27)), ($a, $b);
     -1 * ($c <=> $d);
};

# Use a sorting function that IS a closure.
my $sortfn2 = sub {
     my ($c, $d) = map str2time(substr($_, 27)), ($a, $b);
     $reverser * ($c <=> $d);
};

# Load the data while making everything upper case.
my @data = map uc $_, <DATA>;

# If the the result of using $sortfn2 is different from
# the result of using $sortfn1, this perl interpreter
# is buggy.
my $usingfn1 = join '', sort $sortfn1 @data;
my $usingfn2 = join '', sort $sortfn2 @data;

if ($usingfn1 ne $usingfn2) {
     print "We have a buggy perl here.\n";
}


__DATA__
irongeek         ttypi     Wed Nov 15 17:27
lar              ttyp2     Fri Nov 17 17:12
lar              ttypa     Fri Nov 17 15:53
lar              ttypp     Thu Nov 16 17:11
lar              ttypk     Thu Nov 16 14:20
sabre            ttyp5     Wed Nov 15 13:59
lar              ttyp5     Wed Nov 15 13:57
sabre            ttyp5     Wed Nov 15 13:28
sabre            ttypc     Wed Nov 15 12:10
lar              ttypn     Thu Nov 16 13:23
lar              ttypd     Fri Nov 17 07:27
irongeek         ttyp2     Thu Nov 16 07:49
sabre            ttypg     Sat Nov 11 15:56
-------------OUTPUT FOLLOWS--------------
Perl version: 5.9.4
Date::Parse version: 2.27
We have a buggy perl here.
-------------OUTPUT ENDS-----------------

That program demonstrates that when the sorting subroutine is not a
closure, the lowercasing effect of Date::Parse::str2time is NOT
permitted to modify the original data, and when the sorting subroutine
IS a closure, the lowercasing effect of Date::Parse::str2time IS
permitted to modify the original data.

This is probably a bug in the perl interpreter.

I'm using Debian GNU/Linux 3.1 i386.

---
Flags:
     category=
     severity=
---
Site configuration information for perl 5.9.4:

Configured by (user) at Mon Nov 20 03:07:07 CST 2006.

Summary of my perl5 (revision 5 version 9 subversion 4) configuration:
   Platform:
     osname=linux, osvers=2.4.27-3-386,
archname=i686-linux-thread-multi-64int
     uname='linux dike 2.4.27-3-386 #1 thu sep 14 08:44:58 utc 2006 i686
gnulinux '
     config_args='-Dusethreads -Dprefix=/usr/local/share/perl-5.9
-Duse64bitint -Dcc=gcc -Dusedevel -Duseperlio -Dman1ext=1perl
-Dman3ext=3perl -de'
     hint=recommended, useposix=true, d_sigaction=define
     useithreads=define, usemultiplicity=define
     useperlio=define, d_sfio=undef, uselargefiles=define, usesocks=undef
     use64bitint=define, use64bitall=undef, uselongdouble=undef
     usemymalloc=n, bincompat5005=undef
   Compiler:
     cc='gcc', ccflags ='-D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS
-fno-strict-aliasing -pipe -I/usr/local/include -D_LARGEFILE_SOURCE
-D_FILE_OFFSET_BITS=64',
     optimize='-O2',
     cppflags='-D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS
-fno-strict-aliasing -pipe -I/usr/local/include'
     ccversion='', gccversion='3.3.5 (Debian 1:3.3.5-13)', gccosandvers=''
     intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=12345678
     d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
     ivtype='long long', ivsize=8, nvtype='double', nvsize=8,
Off_t='off_t', lseeksize=8
     alignbytes=4, prototype=define
   Linker and Libraries:
     ld='gcc', ldflags =' -L/usr/local/lib'
     libpth=/usr/local/lib /lib /usr/lib
     libs=-lnsl -ldb -ldl -lm -lcrypt -lutil -lpthread -lc
     perllibs=-lnsl -ldl -lm -lcrypt -lutil -lpthread -lc
     libc=/lib/libc-2.3.2.so, so=so, useshrplib=false, libperl=libperl.a
     gnulibc_version='2.3.2'
   Dynamic Linking:
     dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E'
     cccdlflags='-fpic', lddlflags='-shared -L/usr/local/lib'

Locally applied patches:


---
@INC for perl 5.9.4:
     /usr/local/lib/CPAN/share/perl
     /usr/local/lib/CPAN/lib/perl
     /usr/local/lib/CPAN/lib/site_perl/5.9.4/i686-linux-thread-multi-64int
     /usr/local/lib/CPAN/lib/site_perl/5.9.4
     /usr/local/share/perl-5.9/lib/5.9.4/i686-linux-thread-multi-64int
     /usr/local/share/perl-5.9/lib/5.9.4

/usr/local/share/perl-5.9/lib/site_perl/5.9.4/i686-linux-thread-multi-64int
     /usr/local/share/perl-5.9/lib/site_perl/5.9.4
     .

---
Environment for perl 5.9.4:
     HOME=/home/(user)
     LANG=en_US
     LANGUAGE=en_US:en_GB:en
     LC_CTYPE=en_US.ISO-8859-1
     LD_LIBRARY_PATH (unset)
     LOGDIR (unset)

PATH=/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games:/sbin:/usr/sbin:.:/home/(user)/bin:/usr/local/share/perl-5.9/bin

PERL5LIB=/usr/local/lib/CPAN/share/perl:/usr/local/lib/CPAN/lib/perl:/usr/local/lib/CPAN/lib/site_perl/5.9.4
     PERL_BADLANG (unset)
     SHELL=/bin/bash



-- 
paduille.4060.mumia.w@earthlink.net






Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About