[EMAIL PROTECTED] wrote:

>>    #!/usr/bin/perl -wl
>>    use Math::BigFloat;
>>    $x = 37.75;
>>    print $x - 33.67 - 4.08;
>>    $x = Math::BigFloat->new('37.75');
>>    print $x - 33.67 - 4.08;

This seems to work, although there are some odd line returns

You mean the automatic "\n" with every print? It's because of the -l switch in the #! line. See perlrun(1) manpage:
http://www.perldoc.com/perl5.8.0/pod/perlrun.html#-l%5boctnum%5d


I like to use it in simple programs, as it makes them cleaner without all of those explicit newlines, but you can just remove the -l switch and have a standard print behavior. (Keep the -w, however, it turns on warnings. Or better yet use "use warnings;" pragma.)

and '0' is being returned as '0.'
Still, I can work around that, THANKS!

Yes, indeed. I checked out the source of Math::BigFloat and unfortunately the class name is hardcoded all over the place (it's probably fixed in newer versions) so it's hard to subclass, but this is Perl, after all, so we can redefine the overloaded stringification operator in the Math::BigFloat class itself:


use Math::BigFloat;
{
package Math::BigFloat;
use overload '""' => sub {(my $n = $_[0]->stringify) =~ s/\.$//; $n};
}

Now it prints just "0" without the "." at the end. But note that such a code will stop working if the stringify method in Math::BigFloat is ever renamed. Unlikely, but possible. It's probably not the most elegant way to program, still it's pretty cool.

You could store the \&{'Math::BigFloat::(""'} reference to have the original stringification method even if it's renamed (anyone knows if it's documented somewhere?) but it could introduce another problems with overload.pm implementation.

The code I wrote gives a warning about the ("" subroutine being redefined in overload.pm when the script is run with the -w switch, but (at least on my system) it doesn't print the warning if there's "use warnings;" instead of -w, which is better anyway.

-zsdc.


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to