Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Yesterday broquaint and I were chatting and he mentioned a code snippet like this
sub blessed_regex{ return bless qr/[a-z]/,'foo' }
To which I replied (stupidly) that that wouldnt work, the product of qr// is just a blessed scalar that happens to stringify itself as the regular expression it contains, and that reblessing would destroy the magic. So that it would have to be like this
sub blessed_regex{ return bless \qr/[a-z]/,'foo' }

Well I was wrong! as the code below shows.
use strict; use warnings; use Data::Dumper; my $rex = qr/[A-Z]o[A-Z]/; my $blessed = bless qr/[A-Z]o[A-Z]/,'foo'; $\="\n"; $,=":\t"; print "Rex ",ref $rex; print "Bless",ref $blessed; print "Rex ",$rex,"WoW"=~$rex ? "WoW" : "---"; print "Bless",$blessed,"WoW"=~$blessed ? "WoW" : "---"; print "Rex ",$rex,"wow"=~$rex ? "!WoW" : "---"; print "Bless",$blessed,"wow"=~$blessed ? "!WoW" : "---"; print "Rex ",Dumper($rex); print "Bless",Dumper($blessed); __END__ Rex : Regexp Bless: foo Rex : (?-xism:[A-Z]o[A-Z]): WoW Bless: foo=SCALAR(0x1a7ef64): WoW Rex : (?-xism:[A-Z]o[A-Z]): --- Bless: foo=SCALAR(0x1a7ef64): --- Rex : $VAR1 = qr/(?-xism:[A-Z]o[A-Z])/; Bless: $VAR1 = bless( do{\(my $o = undef)}, 'foo' );
So, the blessed version seems to match correctly, but when stringified returns the stringification of a scalar ref. And Dumper doesnt handle it correctly.

As far as I cant tell from the above results precompiled regexes are in fact base types from the POV of the perl programmer, like real scalars, arrays or hashes or globs. (Actually im sure that the subject of perls types could fill an entire meditiation or five :-) And as far as I can tell there is _no_way_ to determine whether a scalar reference is actually a reference to a real scalar or to a regex.

Can anybody tell me if im wrong, and if so how do I differentiate

bless \"foo","foo"
from
bless qr/somepattern/,"foo";

Many thanks,

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


In reply to Identifying if a variable is the product of a qr// by demerphq

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 examining the Monastery: (4)
As of 2024-03-29 13:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found