Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
hi,

i wrote a package Foo::Expression::Arithmetic, a package Foo::Number and a package Foo::Variable. I used overload to be able to do the following:
my $x1 = Foo::Number->new()->value( 10 ); my $x2 = Foo::Number->new()->value( 20 ); my $x3 = Foo::Number->new()->value( 30 ); my $x4 = Foo::Number->new()->value( 40 ); my $v1 = Foo::Variable->new()->name( 'a' )->value( 50 ); my $c1 = $v1 - $x1 + $x2 + $x3 - $x4; my $c2 = $c1 * $v1;
$c1 and $c2 are now objects of type Foo::Expression::Arithmetic. Foo::Expression::Arithmetic has a output function, so outputing $c1 and $c2 gives me the following:
a - 10 + 20 + 30 - 40      # $c1
a - 10 + 20 + 30 - 40 * a  # $c2
Now i want to be able to do the following:
my $c3 = ( $x1 + $x2 ) * $v1;
not achieve the following output:
( 10 + 20 ) * a   # $c3
use overload '(' => \&left_parenthesis, ')' => \&right_parenthesis,
is not working. :-( Any ideas?

2006-02-07 Retitled by Arunbear, as per Monastery guidelines
Original title: 'overload'


In reply to Using overload to parse arithmetic terms by esskar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-04-19 09:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found