vincentaxhe has asked for the wisdom of the Perl Monks concerning the following question:

just want apply some static type check such as int, I found 'use integer' is global and not flexible ,/^\d+$/ validation is wordy.

such as $a attribute: int

Replies are listed 'Best First'.
Re: way of declare an integer variable instead use /^\d+$/
by tobyink (Canon) on Jun 03, 2024 at 10:07 UTC
Re: way of declare an integer variable instead use /^\d+$/
by LanX (Saint) on May 25, 2024 at 07:14 UTC
      my $x :Int = VALUE is exactly the sugar I want. I use
      my ($text, $page) = split "\t", $line;
      in my addpdfbookmark.pl tool, $line is got from ocr, set '9O' to '90', I was very confused when
      warn $text, ' on page ', $page, ' must be wrong' if $page < $lastpage;
      was triggered, I have to write a valid subroutine,
      sub valid($){ my ($text, $page) = split "\t", shift; $page =~ /^\d+$/ or die 'page not numbers'; warn $text, ' on page ', $page, ' must be wrong' if $page < $lastp +age; $lastpage = $page; return ($text, $page); }
      , It's better to declare it as an integer, But I got 'Invalid SCALAR attribute' error, use what can make it work?
        So your OCR is confusing O and 0 ?

        Typing to integer won't fix that, you will need better OCR or hope you can cover the problems with heuristics.

        > Invalid SCALAR attribute' error,

        Because you need to implement it first, following the links I gave.

        Better don't

        That was an XY problem and implementing this would be a total overkill. :)

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        see Wikisyntax for the Monastery

        It probably does not matter if the O/0 error is in the original .pdf file or if the OCR misreads it. The tr operator (refer tr/SEARCHLIST/REPLACEMENTLIST/cdsr) directly corrects the problem. Your test for a reasonable value is probably a good idea, but it really has nothing to do with the O/0 problem.
        my ($text, $page) = split "\t", $line; $page =~ tr/O/0/ and warn "Replaced 'O' with '0' in page $page";
        Bill
Re: way of declare an integer variable instead use /^\d+$/
by Anonymous Monk on May 25, 2024 at 06:57 UTC
    /^\d+$/ is the shortest you'll get. except maybe int, but that's different.
      someone might disagree, it's no good for perl.
        Perl is, contrary to C, a dynamically typed language.

        A static typing, i.e. at compilation time, is nearly impossible.

        Otherwise you'd need to type everything, including the result of functions. Even then....¹

        A check at runtime is costly, that's why dynamically typed languages are slower, but easier to code.

        As I already said, you could tie the variable with a self-made STORE (This, Value) function which does your checks on Value before storing it.

        Attribute::Handlers has an example for Syntactic sugar to "hide" the tie-ing procedure behind an attribute.

        I'm not aware of any CPAN modules already providing that, especially because there are certainly different models how to react on wrong types ( die , warn , transform, combine)

        I already gave you all the links, you'd need to do it yourself for your own needs.

        For instance 1E0 is a whole number in float notation, opinions will differ.

        I'm saying all of this for completeness, because it's a total overkill for your problem.

        There is no magic wand which can do the heavy lifting for you...

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        see Wikisyntax for the Monastery

        ¹) Perl, like other dynamic languages, does a lot of DWIM typecasting of variables. This doesn't translate well if you want to keep it fast.