Math not working

Discussion in 'Perl Misc' started by Faith Greenwood, Feb 6, 2010.

  1. I'm very confused. I'm pulling some numbers from an xml file and then
    trying to do some math.

    ####XML File
    <library>
    <books>5</books>
    <pages>12</pages>
    </library>

    ####Code
    #!/usr/bin/perl
    use strict;
    use warnings;
    use XML::LibXML;
    use XML::XPath;

    my $parser=XML::LibXML->new();
    my $doc=$parser->parse_file("C:/scripts/production/xml.xml");

    my $books=$doc->find("//books/text()");
    my $pages=$doc->find("//pages/text()");

    print "$books\n";
    print "$pages\n";

    my $total_pages=$books * $pages;
    ########################
    when I run the above code, I get the output:

    5
    12
    Operation "*": no method found,
    left argument in overloaded package XML::LibXML::Nodelist,
    right argument in overloaded package XML::LibXML::Nodelist at
    script.pl line 16



    Why can't I do math operations here?

    thx!
     
    Faith Greenwood, Feb 6, 2010
    #1
    1. Advertising

  2. Faith Greenwood

    John Bokma Guest

    Faith Greenwood <> writes:

    > my $total_pages=$books * $pages;
    > ########################
    > when I run the above code, I get the output:
    >
    > 5
    > 12
    > Operation "*": no method found,
    > left argument in overloaded package XML::LibXML::Nodelist,
    > right argument in overloaded package XML::LibXML::Nodelist at
    > script.pl line 16


    You are trying to multiply two Nodelists...

    > Why can't I do math operations here?


    that's why. You probably have to 1) check that each nodelist has one
    item and 2) extract the text out of each node 3) multiply the text
    (which Perl will automagically convert to numbers)

    --
    John Bokma j3b

    Hacking & Hiking in Mexico - http://johnbokma.com/
    http://castleamber.com/ - Perl & Python Development
     
    John Bokma, Feb 6, 2010
    #2
    1. Advertising

  3. On 2010-02-06, Ben Morrow <> wrote:
    > [1] I consider the fact that fallback => 1 isn't the default to be a bug
    > in the perl overloading system, but it's much too late to change
    > that now. Apart from anything else, any new overloads (like the -X
    > overload that will be in 5.12) must behave as though fallback=>1
    > were specified, for compatibility.


    If you think so, just make overload::simple which has a different
    default for `fallback'.

    In my first experiments, I saw that the behaviour with {fallback =>
    1} was too error-prone - it was very hard for the developer to see
    whether the codepath was through convert-to-Perlish-data methods, or
    through the "specialized operations" methods.

    So I made the default 0.

    Hope this helps,
    Ilya
     
    Ilya Zakharevich, Feb 7, 2010
    #3
  4. On 2010-02-08, Ben Morrow <> wrote:
    >> > [1] I consider the fact that fallback => 1 isn't the default to be a bug
    >> > in the perl overloading system, but it's much too late to change
    >> > that now. Apart from anything else, any new overloads (like the -X
    >> > overload that will be in 5.12) must behave as though fallback=>1
    >> > were specified, for compatibility.


    > [I had forgotten you were the one to originally implement overloading.
    > Just to be clear, the above was in no way intended as any sort of veiled
    > personal attack.]


    Hmm, I do not see anything resembling a personal attack... Anyway,
    the fact that I replied is a good indication that I have not
    considered it so. ;-)

    >> If you think so, just make overload::simple which has a different
    >> default for `fallback'.


    > That doesn't help in this case, where someone has written a class that
    > doesn't use fallback when IMHO it should.


    Definitely. I saw (and still see) no way to find a silver bullet
    which would make things much simpler...

    >> In my first experiments, I saw that the behaviour with {fallback =>
    >> 1} was too error-prone - it was very hard for the developer to see
    >> whether the codepath was through convert-to-Perlish-data methods, or
    >> through the "specialized operations" methods.
    >>
    >> So I made the default 0.


    > I can see that might be a problem, though I think it's one the developer
    > has to deal with anyway.


    With default being 1, it would be much harder for them to see the
    problem. The current way, it is much more probable that *they* would
    see the problem first, so the users would have less problems...

    > Would you agree with me that new overload types
    > *must* default to falling back


    How would "new" types be different from the "old" ones? The problem
    existed back then; what changed?

    > this is then unnecessarily confusing, with some overloads honouring
    > 'fallback' and some not?


    Confusing: yes. "Unnecessarily"? Do not think so. The complexity is
    there, it is not an artificial one...

    Yours,
    Ilya
     
    Ilya Zakharevich, Feb 9, 2010
    #4
  5. On 2010-02-09, Ben Morrow <> wrote:
    >> > Would you agree with me that new overload types
    >> > *must* default to falling back


    >> How would "new" types be different from the "old" ones? The problem
    >> existed back then; what changed?


    > What changed is that there are now published classes that use some
    > overloading, don't specify fallback, and don't overload the new type.
    > Take for example the new "qr" overload. Under 5.10 and earlier, treating
    > an object as a regex would invoke the stringify overload, so 5.12 must
    > continue to do so for objects that don't have a qr overload *even* if
    > fallback was not requested.


    Hmm, I deduce that under "overload types" you meant "overloaded
    operation"? If, yes, of course...

    Ilya
     
    Ilya Zakharevich, Feb 10, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. chirs
    Replies:
    18
    Views:
    795
    Chris Uppal
    Mar 2, 2004
  2. AciD_X
    Replies:
    4
    Views:
    8,153
    Jonathan Turkanis
    Apr 1, 2004
  3. Mark Healey
    Replies:
    7
    Views:
    1,543
    Tim Prince
    May 22, 2006
  4. Philipp
    Replies:
    9
    Views:
    1,166
    Mark Space
    Jul 23, 2008
  5. VK
    Replies:
    15
    Views:
    1,286
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page