Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Math not working

Reply
Thread Tools

Math not working

 
 
Faith Greenwood
Guest
Posts: n/a
 
      02-06-2010
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!



 
Reply With Quote
 
 
 
 
John Bokma
Guest
Posts: n/a
 
      02-06-2010
Faith Greenwood <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Ilya Zakharevich
Guest
Posts: n/a
 
      02-07-2010
On 2010-02-06, Ben Morrow <(E-Mail Removed)> 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
 
Reply With Quote
 
Ilya Zakharevich
Guest
Posts: n/a
 
      02-09-2010
On 2010-02-08, Ben Morrow <(E-Mail Removed)> 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
 
Reply With Quote
 
Ilya Zakharevich
Guest
Posts: n/a
 
      02-10-2010
On 2010-02-09, Ben Morrow <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
Math.min and Math.max for byte / short Philipp Java 9 07-23-2008 12:37 AM
math.h trig functions questions (and some forgotten high school math) Mark Healey C Programming 7 05-22-2006 10:42 AM
Re: Is still math.h the C++ math library ? AciD_X C++ 4 04-01-2004 07:29 PM
Why can I not use: Math a=new Math(); chirs Java 18 03-02-2004 06:00 PM



Advertisments