Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Perl version dependent code (updated)

by haukex (Archbishop)
on Jan 24, 2019 at 20:29 UTC ( [id://1228945]=note: print w/replies, xml ) Need Help??


in reply to Perl version dependent code

How about:

use if $] ge '5.016', feature => 'fc'; use if $] lt '5.016', 'Unicode::CaseFold' => 'fc';

See Unicode::CaseFold. Otherwise you could do:

*case_insens = $] ge '5.016' ? sub { &CORE::fc($a) cmp &CORE::fc($b) } : sub { lc $a cmp lc $b }; @list = sort {case_insens()} @list;

Update: Used &CORE::fc() instead of CORE::fc() so that it really does work on older Perls.

Replies are listed 'Best First'.
Re^2: Perl version dependent code (updated)
by Laurent_R (Canon) on Jan 24, 2019 at 22:50 UTC
    Great idea and nice implementation, but I wonder whether it wouldn't be simpler at this point to use the case_fold function of Unicode::CaseFold in both cases, i.e. irrespective of the Perl version. Something like this (not able to test right now):
    use Unicode::CaseFold qw(case_fold !fc) @list = sort {case_fold($_)} @list;
    (I know this no longer answers the OP exact question, but it seems to me this would be more straight forward.)
      simpler at this point to use the case_fold function of Unicode::CaseFold in both cases

      Sure, TIMTOWTDI :-) Personally I just prefer core Perl when reasonably possible.

      Here's another variant (Unicode::Collate has been in the core since 5.8), although the sorting order is different:

      use Unicode::Collate; @list = Unicode::Collate->new()->sort(@list);
Re^2: Perl version dependent code (updated)
by Aldebaran (Curate) on Feb 03, 2019 at 22:13 UTC

    I've been mulling this over for a bit and expected this to show equality:

    $ ./1.version.pl a is 5.026001 b is v5.26.1 version is v5 version is v5.26.1 c is ж d is Ж did not match $ cat 1.version.pl #!/usr/bin/perl -w use 5.011; use Path::Tiny; use POSIX qw(strftime); use if $] ge '5.016', feature => 'fc'; use if $] lt '5.016', 'Unicode::CaseFold' => 'fc'; my $a = $]; say "a is $a"; my $b = $^V; say "b is $b"; printf "version is v%d\n", $a; # Perl's version printf "version is v%vd\n", $b; # Perl's version my $c = "ж"; say "c is $c"; my $d = "Ж"; say "d is $d"; if (fc($c) eq fc($d)){ say "expressions matched"; } else { say "did not match"; } __END__ $

    The characters are ж and Ж .

      my $c = "ж";

      That's an HTML entity and consists of 7 separate characters. If you typed it like this then hopefully it becomes obvious why it doesn't match against "Ж" which is an entirely different string, folding notwithstanding.

      OTOH, if you didn't type it like this but instead used a unicode literal string such as

      my $c = "ж";
      then your code is missing the absolutely necessary use utf8; pragma in order to decode this character properly.

      (Edited for typo)

        Indeed...

        $ ./1.version.pl 
        a is 5.026001
        b is v5.26.1
        version is v5
        version is v5.26.1
        c is ж
        d is Ж
        expressions matched
        $ cat 1.version.pl 
        #!/usr/bin/perl -w
        use 5.011;
        use Path::Tiny;
        use POSIX qw(strftime);
        use if $] ge '5.016', feature => 'fc';
        use if $] lt '5.016', 'Unicode::CaseFold' => 'fc';
        use utf8;
        use open OUT => ':encoding(UTF-8)', ':std';
        
        my $a = $];
        say "a is $a";
        
        my $b = $^V;
        say "b is $b";
        
        printf "version is v%d\n", $a;  # Perl's version
        printf "version is v%vd\n", $b;  # Perl's version
        
        my $c = "ж";
        say "c is $c";
        
        my $d = "Ж";
        say "d is $d";
        
        if (fc($c) eq fc($d)){
        say "expressions matched";
        
        } else {
            say "did not match";
        }
        __END__ 
        $ 
        
        

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1228945]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-03-29 11:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found