Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: converting strings to ints

by BrowserUk (Patriarch)
on Mar 20, 2010 at 12:31 UTC ( [id://829792]=note: print w/replies, xml ) Need Help??


in reply to converting strings to ints

The warning is there to help you. If you know you are going to be trying to treat null strings as numbers, turn it off:

use strict; use warnings; use 5.010; my @arr1 = ("1", "", ""); my @arr2 = ("", "3", "4"); for my $index (0 .. 2) { no warnings 'numeric'; say $arr1[$index] + $arr2[$index]; } __END__ c:\test>junk 1 3 4

And you can prevent the used only once warning, by using them twice. Eg. add a line $a = $b;.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: converting strings to ints
by AnomalousMonk (Archbishop) on Mar 20, 2010 at 20:20 UTC
    And you can prevent the used only once warning, by using them twice. Eg. add a line $a = $b;.

    use vars qw($a $b); will also quiet the warning.

    But I don't understand why the warning should arise in the first place, especially from the use of such a common module as List::MoreUtils. The warning is not triggered by the solitary presence of  $a and  $b in the comparison block of the sort built-in. If this is an issue with List::MoreUtils and its ilk, shouldn't this be taken care of within the module somehow (but don't ask me how)?

      I honestly don't know for sure, but I assume that as sort is a part of the compiler, it knows what to tweak to ensure that the warning isn't produced. List::Util::reduce() also seems to take care of it. Leastwise the XS version. The PP version also causes the warning, so maybe it's not possible to do from a pure perl module.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
Re^2: converting strings to ints
by Hue-Bond (Priest) on Mar 20, 2010 at 16:35 UTC
    If you know you are going to be trying to treat null strings as numbers, turn it off:

    Then perlcritic will complain:

    Warnings disabled at line 10, column 5.  See page 431 of PBP.  (Severity: 4)

    Solution 1 (ugly): no warnings 'numeric';  ## no critic (TestingAndDebugging::ProhibitNoWarnings)

    Solucion 2 (dangerous): add the following to ~/.perlcriticrc:

    [TestingAndDebugging::ProhibitNoWarnings] allow = numeric

    --
     David Serrano
     (Please treat my english text just like Perl code, i.e. feel free to notify me of any syntax, grammar, style and/or spelling error. Thank you!).

      Then perlcritic will complain:

      Quite frankly my dear, I don't give a damn! :)

      And if you're going to allow a piece of dumb software to override your own judgement, then I've got a link that will tell you your computer is riddled with virii and trojans and will only charge you $49.99 to rid you of them.

      As configured by default, it shouldn't be called perlcritic but perlblatentalarmist.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        As configured by default, it shouldn't be called perlcritic but perlblatentalarmist.

        Yes, I fully agree with that. Thankfully, it can be configured!

        --
         David Serrano
         (Please treat my english text just like Perl code, i.e. feel free to notify me of any syntax, grammar, style and/or spelling error. Thank you!).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-26 02:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found