in reply to Re^2: How to capture the "isn't numeric" warning?
in thread How to capture the "isn't numeric" warning?

Hi harangzsolt33,

It really depends on the use case, you haven't been too clear about this.

What I've shown is a kind of exception handling, (the difference to hippo's solution being that it is only catching warnings and not fatals)

That's ok if you want to monitor complicated code or volatile input.

Or if you have to deal with different unpredictable warnings.

> So, what do you think of my sub?

In this particular case I (like the others replying) have problems to see the advantage over Scalar::Util 's looks_like_number().

Unless you can show a case where the latter fails to avoid the warning.

And you should at least check which warnings were triggered.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

  • Comment on Re^3: How to capture the "isn't numeric" warning?

Replies are listed 'Best First'.
Re^4: How to capture the "isn't numeric" warning?
by harangzsolt33 (Deacon) on Jun 15, 2019 at 16:14 UTC
    Hmm... All right, so it looks like looks_like_number() is better. Usually when I read this forum or ask something, I always learn something and then something extra on top of that. In this case, I learned how to capture warnings, how to turn off warnings. (And don't laugh, my assumption was that you can't just create a block { ... } without having a "for" or "sub" or an "if" keyword in front of it. In other programming languages, you get an error if you just create a block with no keyword in front of it. In perl, this is allowed, and it is something that I just discovered. Lol)

    Thank you for your informative answers!!!

      Even if bare blocks weren't allowed in Perl, you could just use a do { ... } block, or a for loop that only has one iteration.

        > for loop that only has one iteration.

        Side note: Actually that's pretty much how bare blocks are implemented

        perlsyn#Basic-BLOCKs

        > A BLOCK by itself (labeled or not) is semantically equivalent to a loop that executes once. Thus you can use any of the loop control statements in it to leave or restart the block

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      your welcome! :)

      > I always learn something and then something extra on top of that

      That's why the monastery is so great!

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice