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

I have a perl script with the following.

sub unify_information { my ($sid, $rx) = @_; my %uei; # unified extended info my @upi; # unified player info # FIXME unify with {player playername name, other keys/columns} # first process all available player entries for (my $i = 0; exists $rx->{"player_$i"}; $i++) { # add player info to UPI and remove from hash my @player; push @player, $sid; push @player, delete $rx->{"player_$i"} || "Derp"; push @player, delete $rx->{"team_$i"}; push @player, int (delete $rx->{"frags_$i"} || 0); push @player, delete $rx->{"mesh_$i"}; push @player, delete $rx->{"skin_$i"}; push @player, delete $rx->{"face_$i"}; push @player, int (delete $rx->{"ping_$i"} || 0); <line 158 push @player, delete $rx->{"ngsecret_$i"}; push @upi, \@player; } # return remaining values, player array return ($rx, \@upi); } 1;

Argument "Bot" isn't numeric in int at /home/cougarxr7/Programs/MasterServer-Perl-2.4.3/lib/MasterServer/UDP/DatagramProcessor.pm line 158.

Is there any way to stop perl from logging/printing in terminal that argument? Is there a way to tell perl to ignore that error?

Replies are listed 'Best First'.
Re: Argument spamming terminal
by hippo (Archbishop) on Aug 19, 2021 at 20:55 UTC
    Is there a way to tell perl to ignore that error?

    It isn't an error, it's a warning. As such you can mute it in the tightest possible scope.

    #!/usr/bin/env perl use strict; use warnings; my $bot = 'foo'; # This will warn my $foo = int $bot; # This will not { no warnings 'numeric'; my $bar = int $bot; }

    perldoc warnings for more.


    🦛

      Thank you! For correcting me about it being a warning not an error! Using your code stopped the spamming yet it still shows a few line in terminal. Still way much better!

      Using your code I figured out how to stop the warning!

      sub unify_information { my ($sid, $rx) = @_; my %uei; # unified extended info my @upi; # unified player info my $bot = 'foo'; # FIXME unify with {player playername name, other keys/columns} # This will not # first process all available player entries for (my $i = 0; exists $rx->{"player_$i"}; $i++) { # add player info to UPI and remove from hash my @player; push @player, $sid; push @player, delete $rx->{"player_$i"} || "Derp"; push @player, delete $rx->{"team_$i"}; push @player, int (delete $rx->{"frags_$i"} || 0); push @player, delete $rx->{"mesh_$i"}; push @player, delete $rx->{"skin_$i"}; push @player, delete $rx->{"face_$i"}; no warnings 'numeric'; my $bar = int $bot; push @player, int (delete $rx->{"ping_$i"} || 0); no warnings 'numeric'; #my $bar = int $bot; push @player, delete $rx->{"ngsecret_$i"}; push @upi, \@player; } # return remaining values, player array return ($rx, \@upi); } 1;

      By placing your code around the line causing the warning like I did, it stopped printing the warning! Thank You!

        I am glad that you are happy with the outcome. However, it would be remiss of me not to explain a little more about my example code as it seems that perhaps you have not fully grasped its meaning or method of construction.

        Firstly, the braces are important because they form a block which limits the scope of the warnings pragma. This is why in that example you see a warning when assigning to $foo (outside the block) but not to $bar (inside the block, with the scoped pragma). Secondly, the variables $bar, $bot and $foo are only present to illustrate the technique. So, if I were to amend your loop just to silence this one warning, I would do something more like this:

        # first process all available player entries for (my $i = 0; exists $rx->{"player_$i"}; $i++) { # add player info to UPI and remove from hash my @player; push @player, $sid; push @player, delete $rx->{"player_$i"} || "Derp"; push @player, delete $rx->{"team_$i"}; push @player, int (delete $rx->{"frags_$i"} || 0); push @player, delete $rx->{"mesh_$i"}; push @player, delete $rx->{"skin_$i"}; push @player, delete $rx->{"face_$i"}; { # You should put a comment here to say why you are muting these +warnings. no warnings 'numeric'; push @player, int (delete $rx->{"ping_$i"} || 0); } push @player, delete $rx->{"ngsecret_$i"}; push @upi, \@player; }

        In this way the warning is only silenced when trying to call int (delete $rx->{"ping_$i"} || 0) and nothing else in the loop or enclosing subroutine.

        Finally, in silencing the warning you are potentially losing valuable information. Why are you trying to call int on a potentially non-numeric value in the first place? Would it not be better for your code to handle this eventuality properly? Writing robust code will save you time in the long run. Consider spending 5 minutes now to handle non-numeric values of $rx->{"ping_$i"} to save you hours of painful debugging later (especially because you have now silenced the warnings which might otherwise alert you to the problem). Caveat programmer.


        🦛

Re: Argument spamming terminal
by LanX (Saint) on Aug 19, 2021 at 23:22 UTC
    Do you really want to just silence the warning? Are you sure it does what you want?

    I can see plenty of true cases which are not covered by the || 0 part to return 0 on false ...

    DB<6> $rx->{"ping_$i"} = "abc" DB<7> p delete $rx->{"ping_$i"} || 0 abc # not numeric DB<8> p "" || 0 0 # was false DB<9> p undef || 0 0 # was false DB<10> p " " || 0 # " " is true DB<11> p "abc" || 0 abc DB<12> p int "abc" 0 # not numeric evaluates t +o 0 tho in numeric context DB<13> p int "7abc" 7 # beware that leading cip +hers are accepted tho DB<14> use warnings; say int "7abc" Argument "7abc" isn't numeric in int at ... # but will thro a warning + too ... DB<15> p int "" # the || 0 part is redund +ant in all false cases 0 DB<16> p int " " # sic 0 DB<17> p int undef # sic 0 DB<18>

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

Re: Argument spamming terminal
by Marshall (Canon) on Aug 20, 2021 at 20:28 UTC
    I read the posts from Hippo and LanX.
    Instead of ignoring the errors warnings from the int() function, the code to handle the basic cases is not much (shown below). Now instead of seeing warning stuff printed to the terminal, you have some "place holder if statements" where you could put calls to some logging program, etc for the various cases. It does indeed sound odd to convert "ABC" to numeric 0. Note that your code would not convert a blank string " " to zero because that string is logically true.

    If I were debugging this code, I would do an experiment by just taking the call to int() out. Then of course int() can't issue a warning. Presumably that would result in the same type of "non-numeric value used in addition" somewhere else in the code. I would be interested in that code to help me understand why this weird conversion of string to numeric 0 is needed in the first place? Sometimes the immediate runtime error is not the "real" error.

    use strict; use warnings; use feature 'say'; say force2integer(""); # 0 say force2integer(" "); # 0 # " " is actually True say force2integer("abc"); # 0 say force2integer("a10.5"); # 0 say force2integer(12.5); # 12 say force2integer("13"); # 13 say force2integer("14XYZ"); # 0 (would be 14 with a warning) say force2integer(undef); # 0 # Return 0 if the input cannot be converted to an # integer without a warning. # Does not consider malformed floats like: 10.1.2. sub force2integer { my $val = shift; return 0 unless defined $val; return 0 if ($val =~ /[^0-9.]/); # imperfect float test return 0 if ($val eq ""); # null is not numeric 0! return int($val); }