Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^4: Ter::ANSIColor is awesome, but.. (need help)

by Corion (Pope)
on Mar 30, 2014 at 14:15 UTC ( #1080295=note: print w/ replies, xml ) Need Help??


in reply to Re^3: Ter::ANSIColor is awesome, but.. (need help)
in thread Term::ANSIColor is awesome, but.. (need help)

You will need to split up the two uses of $name, for internal program logic and for display.


Comment on Re^4: Ter::ANSIColor is awesome, but.. (need help)
Download Code
Re^5: Ter::ANSIColor is awesome, but.. (need help)
by mascip (Pilgrim) on Mar 30, 2014 at 14:26 UTC

    But this means passing around both variables everywhere, which is a pain.

    Oh! I just realized, I could I not notice? eq() is an operator, I can overload it! Time to Git clone and see if that works...

      I cannot overload eq() because Term::ANSIColor doesn't return an object. Ouch. Are there any alternative implementations out there that return an object?

      It could look like this:

      my $name = String::Colored->new('Jon', 'red'); # improvised module nam +e # Overloads stringification print $name; # Overloads string comparison operators my $res = $name eq 'Jon'; $name->stripcolor;

        Why not implement that yourself?

        package MyApp::User; use strict; use overload '""' => \&stringify, 'eq' => \&cmp, ; sub new { my( $class, %options )= @_; bless \%options => $class; }; sub stringify { my( $self )= @_; colorize( $self->{ name }, 'red' ); }; sub cmp { ... };

        In the long run, this approach of adding more magic instead of cleanly separating display logic and business logic in your program will cause you lots of problems. Most likely you will encounter unintentional stringification. But that's an experience you'll have to make yourself.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1080295]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2014-09-20 19:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (160 votes), past polls