Does perl have a +/- infinity equivalent for string comparisons?

In perl, for numerical comparison, we have +/- infas numbers that are more / less than any other number. Are there also lines that are gt/ ltany other line?

The reason I am asking is because I would like to stick to one or the other at the end of the list of lines to ensure that the loop ends until the end of the list.

+3
source share
3 answers

An empty line or undefless than any other line.

, , . , , ASCII, "\ x80" ( ASCII 128), , .

+2

. "" , chr (~ 0), , . (, 'use locale'.)

(chr (~ 0) no warnings 'utf8', utf8.)

+4

Here's a complete implementation with checks for an overloaded string, which is larger than any other string. This is a fairly simple overload, why joke with approximations?

package String::Infinity;

use overload
    '""'  => sub {
        return "Infinity"
    },
    'cmp' => sub {
        my($left, $right, $reverse) = @_;
        return 0 if ref $right && $right->isa("String::Infinity");
        return $reverse ? -1 : 1;
    },
    fallback => 1
;

sub new {
    my $class = shift;
    return bless \Inf, $class;
}


use Test::More;

my $inf = String::Infinity->new;
is "$inf", "Infinity",  "stringification";
ok $inf eq $inf,        "equals itself"; 
ok $inf ne "foo",       "  doesn't equal anything else";
ok $inf ne "Infinity",  "  even what it stringifies to";
ok $inf gt "lfkdlk",    "  greater than";
ok !$inf lt "lkafj",    "  not less than";
is $inf cmp "lkjd", 1,  "  cmp";
is "ldfjal;kjd" cmp $inf, -1,   "  cmp reversed";

done_testing;
+3
source

Source: https://habr.com/ru/post/1762218/


All Articles