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

ref() and Regexp

by tirwhan (Abbot)
on Jan 31, 2008 at 13:01 UTC ( [id://665339]=perlquestion: print w/replies, xml ) Need Help??

tirwhan has asked for the wisdom of the Perl Monks concerning the following question:

"perldoc -f ref" on my Perl installation (5.8.8) contains:

Builtin types contains: SCALAR ARRAY HASH CODE REF GLOB LVALUE

Note the absence of "Regexp". Yet, when I do

perl -e '$r=qr//; print ref $r'

I get "Regexp" as output. Is this simply missing from the documentation (I note it's included in the perldoc to 5.10)? If so, does anyone know with which Perl version ref started recognising Regexp? Is this reliable for <5.10? (I don't care about overloaded funkyness or objects in this case, this will be used to recognize parameters passed into a function, and if users want to shoot themselves in the foot by passing in weirdness they're welcome ;-). Thanks muchly.


All dogma is stupid.

Replies are listed 'Best First'.
Re: ref() and Regexp
by brian_d_foy (Abbot) on Jan 31, 2008 at 18:27 UTC

    You shouldn't ever have to think about what string ref() returns. By comparing it to a prototype of the same thing, you insulate yourself from version changes and thinkos (like me forgetting if it's REGEX, REGEXP, Regex, or Regexp):

    if( ref $regex eq ref qr// ) { ... }

    Or, hide that in a subroutine is_regex() or something similar.

    --
    brian d foy <brian@stonehenge.com>
    Subscribe to The Perl Review

      Or, hide that in a subroutine is_regex() or something similar.

      Like this?

      BEGIN { if (!defined(&re::is_regexp)) { package re; my $re_class = ref qr//; *is_regexp = sub($) { local *__ANON__ = 'is_regexp'; return UNIVERSAL::isa($_[0], $re_class); }; } }

      Using your philosophy to avoid magical constants, it checks if re::is_regexp is defined rather than checking $] for 5.10.

      BEGIN and the prototype duplicates the behaviour of 5.10's re::is_regexp.

      It uses isa instead of ref so regexes can be made detectable even if there's a need to rebless the regex (by adding Regex to that class's @ISA). It's not an invitation for non-regexes to pretend to be regexes.

      Wouldn't qr/FOO/ be cheaper? (an empty regex is special)
        an empty regex is special
        In a match (see m//), but not for qr//:
        $ perl -e 'print qr//;' (?-xism:)
Re: ref() and Regexp (blessed)
by tye (Sage) on Jan 31, 2008 at 15:06 UTC

    qr// returns a reference blessed into the "Regexp" package (so, no, the documentation for ref is not broken). This is unfortunate since, if you make an object based on a blessed qr// ref, then everthing continues to work but there is no good way to see that the ref is a compiled regex.

    I recall demerphq looking at the proper way (in C / XS) to tell that a ref is the result of qr// for use by Data::Dump::Streamer so you can use the regex() function from that module.

    - tye        

      qr// returns a reference blessed into the "Regexp" package (so, no, the documentation for ref is not broken).

      Aah, ok, so the perl 5.10 pod saying "The result Regexp indicates that the argument is a regular expression resulting from qr//"(emphasis mine), really means the same thing in less detail and nothing has changed between 5.8 and 5.10 in this regard? (I'm assuming this since grinder mentions that an actual change has been made in 5.11)

      if you make an object based on a blessed qr// ref, then everthing continues to work but there is no good way to see that the ref is a compiled regex

      Gotcha, I won't worry about this for now (I'm trying to build something with minimal dependencies), but thank you for the pointer to Data::Dump::Streamer->regex(), that looks really useful.


      All dogma is stupid.

        You can also make an optional dependency on Data::Dump::Streamer rather easily. Using MakeMaker, you'd only document that the user installing Data::Dump::Streamer would allow blessed regexes to be used. Using Module::Build, I think the optional dependency could be declared such that the plethora of automated installation tools can decide to ask the user if they want to install it.

        I don't know what did or didn't change in 5.010, but I'd jump to the same conclusion as you for the same reasons.

        - tye        

Re: ref() and Regexp
by grinder (Bishop) on Jan 31, 2008 at 15:28 UTC

    For what it's worth, REGEXP is now a first-class native type in bleadperl, which will see the light of day as 5.12. Which just goes to show that the reports of the death of Perl 5 are greatly exaggerated :)

    • another intruder with the mooring in the heart of the Perl

      Perl5 isn't just supported, it's actively developed! p5p was talking about new features for 5.12 the day after 5.10 was released, if not the day of!

        Heh! Well, if it comes to that, features for 5.12 were already being discussed before 5.10 came out.

        • another intruder with the mooring in the heart of the Perl

Re: ref() and Regexp
by hipowls (Curate) on Jan 31, 2008 at 13:36 UTC

    This is the output from perl 5.6.1

    michael$ perl -v This is perl, v5.6.1 built for sun4-solaris-64int (with 48 registered patches, see perl -V for more detail) Copyright 1987-2001, Larry Wall Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5 source ki +t. Complete documentation for Perl, including FAQ lists, should be found +on this system using `man perl' or `perldoc perl'. If you have access to + the Internet, point your browser at http://www.perl.com/, the Perl Home Pa +ge. michael$ perl -le'print ref qr/abc/' Regexp

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://665339]
Approved by moritz
Front-paged by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-03-28 13:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found