Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Internal representation of qr// compiled regular expressions

by John M. Dlugosz (Monsignor)
on Jun 27, 2001 at 22:02 UTC ( #92008=perlquestion: print w/replies, xml ) Need Help??
John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:

I was thinking of "overloading" a function to tell if an argument was a RE vs. a string by having the user use qr// to pass the RE. So what is the result I see? The perlfunc page on ref doesn't mention that at all on the list of built-in types. So trying
perl -e "print qr/a/"
gives a result of Regexp, which is not terribly surprising, but then I wonder why isn't it all caps, "REGEXP", like the other built-in types?

Is the precompiled RE implemented using a class, something that the qr// operator knows about? Does this mean that Regexp (not all-capped) is reserved as a package name? Are there any interesting members in there now...?


Replies are listed 'Best First'.
Re: Internal representation of qr// compiled regular expressions
by japhy (Canon) on Jun 27, 2001 at 22:07 UTC
    qr// returns an object of the class Regexp. You can make methods for them like any other object. In addition, Perl knows that if it sees /$foo/, and $foo is an object of Regexp, it doesn't need to compile the regex, it uses the information stored in the object.
    jpinyan@sushi [2:02pm] ~ #501> perl -MDevel::Peek -e 'Dump(qr/a+bc?/)' SV = RV(0xf18d0) at 0xe46f0 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0xe4798 SV = PVMG(0xf0a78) at 0xe4798 REFCNT = 1 FLAGS = (OBJECT,RMG) IV = 0 NV = 0 PV = 0 MAGIC = 0xf2158 MG_VIRTUAL = 0xe336c MG_TYPE = 'r' MG_OBJ = 0xf0ef0 STASH = 0xef128 "Regexp"

    japhy -- Perl and Regex Hacker
(tye)Re: Internal representation of qr// compiled regular expressions
by tye (Sage) on Jun 27, 2001 at 22:35 UTC

    Go to the p5p archive and do a case-sensitive search for "ref Regexp" (for example) to find quite a few interesting items on this subject.

    There have been attempts to change it to Regex, REGEX, or REGEXP.

    Also, note that UNIVERSAL::isa(qr/hi/,"SCALAR") returns true so that the regex is actually a blessed reference to a scalar (yes, I peek under the covers all the time).

            - tye (but my friends call me "Tye")
      A regex object is actually a blessed reference to a scalar: interesting. That's what japhy's dump shows, too. But what's in that scalar? It seems to have a overloaded "" that decodes the compiled expression back into a string, but the dump, as does an explicit ${qr/a/}, shows that nothing is stored in that scalar. It's not a pointer to a C structure, or byte code in the SV, or whatnot. It's a PVMG, whatever that is (not documented in Devel::Peek nor perlguts).


        pp_ctl.c (in pp_regcomp()) shows that, if the regex has magic on (which means it's a qr// regex), it extracts the regexp struct from mg->mg_obj. So that's where the "meat" of a Regexp object is.

        Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
        How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://92008]
Approved by root
[erix]: yes, that's kinda early-ish too
[stonecolddevin]: i remember finding an ubuntu 7.* CD sitting around and taking it. I had that thing up until like 4 years ago i think
[mr_mischief]: https://groups. forum/#!msg/comp. os.minix/ dlNtH7RRrGA/ SwRavCzVE7gJ
[mr_mischief]: https://groups. forum/#!msg/comp. os.minix/ dlNtH7RRrGA/ SwRavCzVE7gJ
[mr_mischief]: 1991/08/25 is when he announced on Usenet he was working on an OS
[erix]: heh, nice mr_mischief
[mr_mischief]: I remember trying Yggdrasil and SoftLandimg System around 1992.
[mr_mischief]: Maybe 1993 actually. I think my Yggdrasil copy came from the Monster Media Best of 1993 Freeware/Shareware CD collection

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (10)
As of 2018-07-17 17:37 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (374 votes). Check out past polls.