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

I use XML::Smart several times, I like this xml module, It is very easy to use! but i meet a strange problem. when i get a numeric value from xml file which save some old status value, and use the current status numeric value subtract the old value, the value is negative with the true result. for example:
xml file: <global_status> ...... <status name="Uptime" value="816078" value_var="30"/> </global_status> code: my $now_value = scalar($_->[1]); my $last_value = $last_status_data->{value}; $value_var = $now_value - $last_value;
I print three value: name: Uptime, last_value: 816078, now_value: 816125, value_var: -47 the true result should be 47, but i get -47. I can solve the problem with:
my $last_value = $last_status_data->{value}+0;
but i want to know why the negative result is generated? Thanks a lot! Robin

Replies are listed 'Best First'.
Re: why the subtract result is negative when i use XML::Smart!
by tye (Sage) on Aug 20, 2007 at 04:23 UTC

    It sounds exactly like a trivial bug in a module that overloads subtraction. But it doesn't appear to me that XML::Smart does that.

    And the problem is with the $last_status_data->{value} result. You seem to me to strongly imply that $now_value is coming from XML::Smart, but I find no clear indication as to what type of object $last_status_data is. That is where you need to look.

    - tye        

Re: why the subtract result is negative when i use XML::Smart!
by GrandFather (Saint) on Aug 20, 2007 at 03:59 UTC

    Try writing a short program that demonstrates the problem. With what you have provided it's not clear where most of the variables are generated or how and there is nothing anywhere to suggest that 47, of either sign, is the right answer.


    DWIM is Perl's answer to Gödel
      A short snip code as below to demo the problem,the true result should be 730,but in fact i get -730. I use the newest Release XML-Smart-1.6.9. I doubt the problem is from the module: Object::MultiType.
      [root@dw01 xml]# ls last.xml test.pl [root@dw01 xml]# more last.xml <?xml version="1.0" encoding="iso-8859-1" ?> <?meta name="GENERATOR" content="XML::Smart/1.6.9 Perl/5.008008 [linux +]" ?> <global_status> <status name="Uptime" value="4630" value_var="-861958"/> </global_status> [root@dw01 xml]# more test.pl #! /usr/bin/perl use XML::Smart; #get last status my $xml_last_status = XML::Smart->new('last.xml'); my $last_status_data = $xml_last_status->{global_status}->{status}[0]; my $last_value = $last_status_data->{value}; my $now_value = 5360; my $value_var = $now_value - $last_value; print "last_value: $last_value, now_value: $now_value, value_var: $val +ue_var\n"; [root@dw01 xml]# ./test.pl last_value: 4630, now_value: 5360, value_var: -730 [root@dw01 xml]#

        It looks like a bug in Object::MultiType. $last_value is an XML::Smart object (which derives from Object::MultiType) and overloads subtraction. Looks like line 168 ($n -= $_[0] ;) in the sub _OVER_deinc in multitype.pm may have it backwards. Changing the last two line of the sub to:

        my $temp = scalar $_[0]; return $temp - $n;

        fixes the problem for this case. I can't guess what situation it may fail for though.

        You should raise a bug tag against the module perhaps?

        You can do that at rt://Object::MultiType


        DWIM is Perl's answer to Gödel