Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
This was a cool article. Dont know how useful, but very interesting.
I am sure I will find a use for it somewhere.

I tried to play with blessing RegEx's and ran into trouble.
You mention that you might be able to bless a s{}{}, but I
couldn't figure it out.

I wanted an object that would do "s/^\s+|\s+$/g" for me.
It would not compile if i did somethine like
$self = s{^\s+|\s+$}{}; so I ended up using qr{} instead,
putting the s/// in the member function:
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->clean($foo); print $foo, "\n";
So this works, but I dont know it is the most optimized
routine. I did some benchmarks:
package main; $c = new Cleaner; $foo = "\t\tFOO\t\t"; use Benchmark; timethese(1000000, { 'blessed' => sub { my $bar = $foo; $c->clean($bar) }, 'normal' => sub { my $bar = $foo; $bar =~ s/^\s+|\s$//g }, });
And got these results:
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)
I guess the difference is just the function call, but thought maybe there is a faster way. Any Ideas?

In reply to RE: Blessables -- What Can You Make Into Objects? by perlmonkey
in thread Blessables -- What Can You Make Into Objects? by chromatic

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (10)
    As of 2015-07-31 19:06 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (280 votes), past polls