CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Perl 5.10 not backwards compatible with 5.8

 



char_boy
Novice

Nov 21, 2010, 5:39 AM

Post #1 of 14 (2181 views)
Perl 5.10 not backwards compatible with 5.8 Can't Post

The following script runs on 5.8 but does not run on 5.10.


Code
use strict; 
my %SAM;
$SAM{chr2LHet} = 1;
$SAM{chr2RHet} = 1;
$SAM{chr3LHet} = 1;
$SAM{chr3RHet} = 1;
foreach my $chr (sort cmpChrs keys %SAM) {
print "CHR=$chr\n";
}
sub cmpChrs () {
if($a =~ /chr(\d+)/) {
my $numa = $1;
if($b =~ /chr(\d+)/) {
my $numb = $1;
if($numa < $numb) { return 1; }
else {
$a =~ s/chr\d+//;
$b =~ s/chr\d+//;
my %temp;
$temp{$a}=1;
$temp{$b}=1;
foreach my $key (sort cmpChrs keys %temp) {
if($key eq $a) { return 1; }
else { return -1; }
}
}
} else { return 1; }
}
return 1;
}

On 5.8 it does not crash and outputs the following:
CHR=LHet
CHR=RHet
CHR=LHet
CHR=RHet

On 5.10 it crashes with the following message:

Can't undef active subroutine at test.pl line 29.
Attempt to free unreferenced scalar: SV 0x1124fa0, Perl interpreter: 0x10f7010 at test.pl line 29.
Attempt to free unreferenced scalar: SV 0x1124fa0, Perl interpreter: 0x10f7010 at test.pl line 29.


I had to do the following to fix it:

Code
use strict; 
my %SAM;
$SAM{chr2LHet} = 1;
$SAM{chr2RHet} = 1;
$SAM{chr3LHet} = 1;
$SAM{chr3RHet} = 1;
foreach my $chr (sort {cmpChrs($a,$b)} keys %SAM) {
print "CHR=$chr\n";
}
sub cmpChrs () {
if($a =~ /chr(\d+)/) {
my $numa = $1;
if($b =~ /chr(\d+)/) {
my $numb = $1;
if($numa < $numb) { return 1; }
else {
$a =~ s/chr\d+//;
$b =~ s/chr\d+//;
my %temp;
$temp{$a}=1;
$temp{$b}=1;
foreach my $key (sort {cmpChrs($a,$b)} keys %temp) {
if($key eq $a) { return 1; }
else { return -1; }
}
}
} else { return 1; }
}
return 1;
}


I always thought that if a new version was not backwards compatible that they'd change the first number, so this should be Perl 6.x not 5.10.


(This post was edited by char_boy on Nov 21, 2010, 5:46 AM)


FishMonger
Veteran / Moderator

Nov 21, 2010, 8:34 AM

Post #2 of 14 (2170 views)
Re: [char_boy] Perl 5.10 not backwards compatible with 5.8 [In reply to] Can't Post

Why are you using an empty prototype?

Why are you altering the key within the sort routine? You should not do that.

Here's much more simplified version (if you want it backwards compatible, then remove the use statement and use print instead of say).


Code
#!/usr/local/bin/perl 

use warnings;
use strict;
use 5.10.0;

my %SAM;
$SAM{chr12RHet} = 1;
$SAM{chr13LHet} = 1;
$SAM{chr2LHet} = 1;
$SAM{chr2RHet} = 1;
$SAM{chr3LHet} = 1;
$SAM{chr3RHet} = 1;

for my $chr ( sort { cmpChrs($a, $b) } keys %SAM ) {
say $1 if $chr =~ /chr\d+(.+)/;
}


sub cmpChrs {
($a =~ /(\d+)/)[0] <=> ($b =~ /(\d+)/)[0];
}


or drop the sub definition


Code
for my $chr ( sort { ($a =~ /(\d+)/)[0] <=> ($b =~ /(\d+)/)[0] } keys %SAM ) { 
say $1 if $chr =~ /chr\d+(.+)/;
}



FishMonger
Veteran / Moderator

Nov 21, 2010, 8:41 AM

Post #3 of 14 (2168 views)
Re: [char_boy] Perl 5.10 not backwards compatible with 5.8 [In reply to] Can't Post

BTW, that sort routine was pulled from the perldoc.

perldoc -f sort

Code
.... 
....
# inefficiently sort by descending numeric compare using
# the first integer after the first = sign, or the
# whole record case-insensitively otherwise

@new = sort {
($b =~ /=(\d+)/)[0] <=> ($a =~ /=(\d+)/)[0]
||
uc($a) cmp uc($b)
} @old;

# same thing, but much more efficiently;
# we'll build auxiliary indices instead
# for speed
@nums = @caps = ();
for (@old) {
push @nums, /=(\d+)/;
push @caps, uc($_);
}

@new = @old[ sort {
$nums[$b] <=> $nums[$a]
||
$caps[$a] cmp $caps[$b]
} 0..$#old
];

# same thing, but without any temps
@new = map { $_->[0] }
sort { $b->[1] <=> $a->[1]
||
$a->[2] cmp $b->[2]
} map { [$_, /=(\d+)/, uc($_)] } @old;
....
....



char_boy
Novice

Nov 21, 2010, 10:10 AM

Post #4 of 14 (2164 views)
Re: [FishMonger] Perl 5.10 not backwards compatible with 5.8 [In reply to] Can't Post


In Reply To
Why are you using an empty prototype?

Why are you altering the key within the sort routine? You should not do that.

Here's much more simplified version (if you want it backwards compatible, then remove the use statement and use print instead of say).

It seems like you missed my point. I distilled out a short program with the heart of the bug. Such a thing should never be interpreted literally like you have. My real sort routine *does* need to be recursive, but I removed most of the code. Ignore the fact that this doesn't achieve something useful as is, the point is it runs on 5.8 and not on 5.10. How can that be tolerated? It seems to me 5.10 should be fully backwards compatible with 5.8


FishMonger
Veteran / Moderator

Nov 21, 2010, 10:39 AM

Post #5 of 14 (2162 views)
Re: [char_boy] Perl 5.10 not backwards compatible with 5.8 [In reply to] Can't Post

P5P tries and is very good about maintaining backwards compatibility, but is some cases things do get broken, or a previous bug gets fixed which in turn might break some code that didn't take into account that previous bug.

I'm not ready to say that you've found a bug. That question would be best answered by the P5P group.

http://lists.perl.org/list/perl5-porters.html

If you add the diagnostics pragma, you'll get more descriptive error.


Code
D:\perl>char_boy.pl 
Can't undef active subroutine at D:\perl\char_boy.pl line 34 (#1)
(F) You can't undefine a routine that's currently running. You can,
however, redefine it while it's running, and you can even undef the
redefined subroutine while the old routine is running. Go figure.

Uncaught exception from user code:
Can't undef active subroutine at D:\perl\char_boy.pl line 34.
at D:\perl\char_boy.pl line 34
main::cmpChrs called at D:\perl\char_boy.pl line 12
Attempt to free unreferenced scalar: SV 0x1a39c0, Perl interpreter: 0x15da508 at C:/Perl64/lib/Carp.pm line 45.
Attempt to free unreferenced scalar: SV 0x1a39c0, Perl interpreter: 0x15da508 at D:\perl\char_boy.pl line 34.



char_boy
Novice

Nov 21, 2010, 10:56 AM

Post #6 of 14 (2160 views)
Re: [FishMonger] Perl 5.10 not backwards compatible with 5.8 [In reply to] Can't Post

Thanks I'll post it there. I may have been doing something that's not legitimate and now isn't allowed. Strange though.

The strangest thing is that it has no problem with the first two top level calls of the subroutine where it does call itself recursively, and it has no problem with the third top level call where it does not call itself recursively. It crashes on the fourth top level call where it also does not call itself recursively and is basically the same as the previous time it was called. So that's very strange and inconsistent behavior leading me to believe it is not intended. But I'm really not an expert.


FishMonger
Veteran / Moderator

Nov 21, 2010, 1:43 PM

Post #7 of 14 (2154 views)
Re: [char_boy] Perl 5.10 not backwards compatible with 5.8 [In reply to] Can't Post

I don't have enough info about your script to say if this is true, but to me based on your example code the real problem seems to be a flaw in your design of the hash structure. Maybe a more complex structure, such as a HoH or HoHoH or something similar would be better.

If you don't mind, could you post a follow up with the answer from P5P, or a link to the thread?


rovf
Veteran

Nov 22, 2010, 7:28 AM

Post #8 of 14 (2133 views)
Re: [char_boy] Perl 5.10 not backwards compatible with 5.8 [In reply to] Can't Post


Quote
I always thought that if a new version was not backwards compatible that they'd change the first number


I wouldn't count on this. While perldelta for 5.8.8 explicitly says:

There are no changes intentionally incompatible with 5.8.7. If any exist, they are bugs and reports are welcome.

this sentence is missing in perldelta for 5.10.0

Perhaps the compatibility guarantee applies only for changes in the last figure.


char_boy
Novice

Nov 22, 2010, 8:50 AM

Post #9 of 14 (2126 views)
Re: [rovf] Perl 5.10 not backwards compatible with 5.8 [In reply to] Can't Post

Thanks guys I'll post what I find out from P5P but that uses old school news readers which I'm not set up for, trying to figure it out now. Can't they just use web forums it isn't 1995 anymore.


FishMonger
Veteran / Moderator

Nov 22, 2010, 9:04 AM

Post #10 of 14 (2123 views)
Re: [char_boy] Perl 5.10 not backwards compatible with 5.8 [In reply to] Can't Post

Google has a portal to the news group.

http://groups.google.com/group/perl.perl5.porters/topics?msg=subscribe&hl=en


FishMonger
Veteran / Moderator

Nov 22, 2010, 9:08 AM

Post #11 of 14 (2120 views)
Re: [char_boy] Perl 5.10 not backwards compatible with 5.8 [In reply to] Can't Post

You might want to post your question on one of the other news groups before posting to P5P.

Here's one I'd suggest. Most of the main contributors are well known cpan and book authors, including Randal Schwartz.
http://groups.google.com/group/perl.beginners/topics?hl=en


(This post was edited by FishMonger on Nov 22, 2010, 9:08 AM)


char_boy
Novice

Nov 22, 2010, 9:54 AM

Post #12 of 14 (2116 views)
Re: [FishMonger] Perl 5.10 not backwards compatible with 5.8 [In reply to] Can't Post


In Reply To
You might want to post your question on one of the other news groups before posting to P5P.

Here's one I'd suggest. Most of the main contributors are well known cpan and book authors, including Randal Schwartz.
http://groups.google.com/group/perl.beginners/topics?hl=en

Thanks I posted there, it said it has to be approved by moderators, so I wait...


char_boy
Novice

Nov 23, 2010, 10:07 AM

Post #13 of 14 (2076 views)
Re: [char_boy] Perl 5.10 not backwards compatible with 5.8 [In reply to] Can't Post


In Reply To
You might want to post your question on one of the other news groups before posting to P5P.

I submitted it about 24 hours ago but it never posted, how long does the moderator usually take for such a thing? Do you think they rejected my question for posting?


FishMonger
Veteran / Moderator

Nov 23, 2010, 10:50 AM

Post #14 of 14 (2073 views)
Re: [char_boy] Perl 5.10 not backwards compatible with 5.8 [In reply to] Can't Post

I had that same 24hr+ delay problem the first few times I posted via google's portal. If you send a subscribe email directly to the list, then your posts will show up very quickly.

http://lists.perl.org/list/beginners.html

FAQ for the beginners mailing list
http://learn.perl.org/faq/beginners.html

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives