in reply to way of declare an integer variable instead use /^\d+$/

Your question is a bit unclear, an SSCCE would help.

I have to guess...

You could use Tie::Scalar to make sure that a var $x will always stay an integer, by declaring STORE. But that will slow down every operation on $x.

And you can define an attribute :Int for that° as syntactic sugar.

my $x :Int = VALUE

Compare https://perldoc.perl.org/Attribute::Handlers#Attributes-as-tie-interfaces

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

Update

°) that Tie::Scalar class you define

Replies are listed 'Best First'.
Re^2: way of declare an integer variable instead use /^\d+$/
by vincentaxhe (Scribe) on May 25, 2024 at 09:25 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

        The ocr is named cnocr, it is the best I can get, It's trained specific for chinese under python, it's much accurate compared with tesseract.

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

        where is the link about the sugar? is it not simple as add 'use ...'? I'm using 5.38 version under arch.declare a integer can be a very useful method in all ways.

      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