<?xml version="1.0" encoding="windows-1252"?>
<node id="8314" title="RE: Blessables -- What Can You Make Into Objects?" created="2000-04-21 01:30:44" updated="2005-08-06 03:16:42">
<type id="11">
note</type>
<author id="7995">
perlmonkey</author>
<data>
<field name="doctext">
This was a cool article.  Dont know how useful, but very interesting.&lt;BR&gt;
I am sure I will find a use for it somewhere.&lt;BR&gt;
&lt;BR&gt;
I tried to play with blessing RegEx's and ran into trouble.&lt;BR&gt;
You mention that you might be able to bless a s{}{}, but I&lt;BR&gt;
couldn't figure it out.&lt;BR&gt;
&lt;BR&gt;
I wanted an object that would do "s/^\s+|\s+$/g" for me.&lt;BR&gt;
It would not compile if i did somethine like&lt;BR&gt;
$self = s{^\s+|\s+$}{};  so I ended up using qr{} instead,&lt;BR&gt;
putting the s/// in the member function:
&lt;CODE&gt;
package Cleaner;
sub new 
{
    my $class = shift;
    $class = ref($class) || $class;
    my $self = qr{^\s+|\s+$};
    return bless($self, $class);
}

sub clean 
{
    $_[1] =~ s/$_[0]//g;
}

package main;
$c = new Cleaner;
$foo = "\t\tFOO\t\t";
$c-&gt;clean($foo);
print $foo, "\n";
&lt;/CODE&gt;

So this works, but I dont know it is the most optimized&lt;BR&gt;
routine.  I did some benchmarks:
&lt;CODE&gt;
package main;
$c = new Cleaner;
$foo = "\t\tFOO\t\t";
use Benchmark;
timethese(1000000, {
    'blessed' =&gt; sub { my $bar = $foo; $c-&gt;clean($bar) },
    'normal'  =&gt; sub { my $bar = $foo; $bar =~ s/^\s+|\s$//g },
});
&lt;/CODE&gt;
And got these results:
&lt;CODE&gt;
Benchmark: timing 1000000 iterations of blessed, normal...
   blessed: 43 wallclock secs (36.50 usr +  0.14 sys = 36.64 CPU)
    normal: 28 wallclock secs (24.19 usr +  0.08 sys = 24.27 CPU)
&lt;/CODE&gt;
I guess the difference is just the function call, but thought
maybe there is a faster way.  Any Ideas?


</field>
<field name="root_node">
8259</field>
<field name="parent_node">
8259</field>
</data>
</node>
