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

With perl 5.8.8 in debian etch using mod_perl in apache, I have the following happening:
 my $hash = RRDs::info( $datafile );
 my $step = $hash->{'step'};
A string of '300' is returned. When used in:
my $mid = $lupdate % $step ;
I get an 'Illegal modulus zero' error. It is as though the string of '300' is not being converted to numeric. Yet if I
my $step = '300';
The '%' will operator will work as expected. Am I missing something with the RRDs library? Some taint mode or something?

Replies are listed 'Best First'.
Re: implicit numeric conversion isn't
by dave_the_m (Monsignor) on Nov 25, 2006 at 00:13 UTC
    Perhaps the variable doesn't contain exactly what you think it does (eg invisible control characters). Or perhaps it's some wierd object with an overloaded numification operator. Try adding this on the line before to see exactly what it is:
    use Devel::Peek; Dump $step; my $mid = $lupdate % $step ;

    Dave.

      Thanx for the tip. The following code
        my $hash = RRDs::info( $datafile );
        my $hStep = $hash->{'step'};
      Dump $hStep;
      $hStep='301';
      Dump $hStep;
      
      PV shows what I want in each case, but the two have different FLAGS. Any of them make any sense to you. generates:
      SV = PVIV(0x8b82158) at 0x832c55c
        REFCNT = 1
        FLAGS = (PADBUSY,PADMY,IOK,POK,pIOK,pPOK)
        IV = 300
        PV = 0x8c2ee40 "300"\0
        CUR = 3
        LEN = 4
      SV = PVIV(0x8b82158) at 0x832c55c
        REFCNT = 1
        FLAGS = (PADBUSY,PADMY,POK,pPOK)
        IV = 300
        PV = 0x8c2ee40 "301"\0
        CUR = 3
        LEN = 4
      
      
        Neither of those variables that Dump() has displayed would cause the error you see. It would be fine to use either of those 2 with the modulus operator.

        Somehow, your program is seeing some other value that equates to (or is) zero. (Are warnings enabled ?)

        What's happening between  my $step = $hash->{'step'}; and my $mid = $lupdate % $step ; in your code ?

        Cheers,
        Rob
        The first dump shows that the value of $hStep was set to the string "300" and converted to numeric (or vice versa.) My copy of RRDtool doesn't do that; only IOK is set and not POK. I'm using version 1.2.12 of RRDtool, the current is 1.2.15. You might want to check that you have a reasonably recent version of RRDtool, but as others have pointed out, that certainly isn't a direct cause of your zero modulus error.
Re: implicit numeric conversion isn't
by quester (Vicar) on Nov 25, 2006 at 00:13 UTC
    It shouldn't be taint; taint is checked for system calls, but not for simple operations like modulus.

    It does work okay here (RHEL 3, perl 5.8.0).

    When I run it as follows:

    use strict; use warnings; use RRDs; my $datafile = "temp.rrd"; my $lupdate = 12345; my $hash = RRDs::info($datafile); my $step = $hash->{'step'}; my $mid = $lupdate % $step; print $mid;
    ... it prints 45 as expected.

    Possibly the error is elsewhere in your code...?

      Try it this way (get last update from the hash as well):
        my $hash    = RRDs::info( $datafile );
        my $hStep   = $hash->{'step'};
        my $lupdate = $hash->{'last_update'};
        my $mid     = $lupdate % $hStep;
        print( "$lupdate % $hStep = $mid\n" );
      
        That works here, too. It prints (at the moment) 187.   Devel::Peek shows both $hStep and $lupdate are integers (IOK set):
        SV = IV(0x9f83208) at 0x9ee8b6c
          REFCNT = 1
          FLAGS = (PADBUSY,PADMY,IOK,pIOK)
          IV = 300
        SV = IV(0x9f8320c) at 0x9ee8b78
          REFCNT = 1
          FLAGS = (PADBUSY,PADMY,IOK,pIOK)
          IV = 1163531587
        
Re: implicit numeric conversion isn't
by Joost (Canon) on Nov 25, 2006 at 00:13 UTC
      Thanx for your response. If I
      print( "'" . ref($step) . "'\n" );
      
      I get nothing. I made another post where I did a Dump. Does it make any sense?
        Yes - that does make sense - $step is NOT a reference, so you get nothing.

        Here are a couple of somewhat lame suggestions - but if you are desparate enough, you may want to try these:

        • Use Data::Dumper, and print Dumper $hash;
        • Try using RRDs::fetch instead of info - This will probably give you more info than you want, but if info isnt working for you ....

             "A closed mouth gathers no feet." --Unknown

Re: implicit numeric conversion isn't
by Anonymous Monk on Nov 25, 2006 at 08:11 UTC
    Well, another embarrassing bug solved. I was looping through a bunch of structures and, wouldn't you know it, one of the structures at the end didn't have stuff in it, which caused the zero size. Based upon what my testing web page was telling me, I thought the problem was at the beginning rather than the end. Thanx for your suggestions, I learned a few more things today.