Math not working

  • Thread starter Faith Greenwood
  • Start date
F

Faith Greenwood

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!
 
J

John Bokma

Faith Greenwood said:
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)
 
I

Ilya Zakharevich

[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
 
I

Ilya Zakharevich

[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. ;-)
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...
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
 
I

Ilya Zakharevich

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top