Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
There's more than one way to do things
 
PerlMonks  

Re: Re: Blessables -- What Can You Make Into Objects?

by Anonymous Monk
on Apr 26, 2002 at 21:59 UTC ( #162416=note: print w/ replies, xml ) Need Help??


in reply to Re: Blessables -- What Can You Make Into Objects?
in thread Blessables -- What Can You Make Into Objects?

You could overload stringification in your object that blesses the regexp.

package Blessed::Re; use overload q{""} => 'stringify'; sub new { my $class = shift; my $word = shift; my $self = qr/\[$word\]/; return bless $self, $class; } sub stringify { my $self = shift; my $string = overload::StrVal $self; $string =~ s/SCALAR/Regexp/; return $string; } package main; my $x = Blessed::Re->new('word'); print "Class: ",ref $x,"\n"; print "Type: ", $x =~ /=(\w+)\(/, "\n";

On the other hand, you did say "cleanly" ...


Comment on Re: Re: Blessables -- What Can You Make Into Objects?
Download Code
Re: Re: Re: Blessables -- What Can You Make Into Objects?
by demerphq (Chancellor) on Apr 26, 2002 at 22:06 UTC
    Actually stringifying a regex does not return a string that contains "Regexp". It will look like any other scalar reference that has been stringified. (Youre thinking of what ref() would return.)

    Thats part of the problem...

    :-)

    Yves / DeMerphq
    ---
    Writing a good benchmark isnt as easy as it might look.

      But that's why I overloaded stringification to contain 'Regexp' instead of 'SCALAR', because we still want ref() to return the classname and we want some way to determine of our blessed object is a Regexp type right?
        Ah. I should have read your code closer. Apologies.

        Nice trick, but I need this to work for any arbitrary classname. This is for a new Data::Dumper module. Er, for that matter Data::Dumper has the same problem:

        use Data::Dumper; my $x=bless qr/dedum/,'foo'; print Dumper($x); __END__ $VAR1 = bless( do{\(my $o = undef)}, 'foo' );

        Yves / DeMerphq
        ---
        Writing a good benchmark isnt as easy as it might look.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (12)
As of 2014-04-18 21:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (472 votes), past polls