Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Comment on

( #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"
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

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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?

    What's my password?
    Create A New User
    [choroba]: My recent record is 10km in 1 hour :)
    [perldigious]: Nice erix... perldigious' legs are so sore from portaging his kayak from place to place last weekend he can barely hobble 12m. :-)
    [erix]: you're faster than me then :)
    [erix]: well, at the moment, that is :)
    [erix]: I should lose some weight although I keep telling myself that I'm doing extra training by lugging it along
    [erix]: I saw a common kingfisher and a couple of bullfinches
    [erix]: bullfinch ( a male and a female )
    erix learned the word 'portaging' not so long ago (from vikings lugging their ships from one river-system to another)
    [choroba]: my best run so far

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (11)
    As of 2017-05-24 12:46 GMT
    Find Nodes?
      Voting Booth?