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

Why is $$r not defined? I'm not sure what value I expected to find at the end of ${qr//} but I didn't think it'd be undef.

use strict; use warnings; my $r = qr/Delectable/; print defined $$r ? "Defined" : "Not defined";

Replies are listed 'Best First'.
Re: Undefined regexp objects?
by ihb (Deacon) on Jan 16, 2003 at 00:12 UTC
    For those that wonder how the heck you even can dereference a qr// object: I'm sorry, I don't know either. :)

    But this below is usually how I go about when I find an unexpected behaviour of this kind. I perhaps won't get all the whys, but I might get an explanation that works for me and that will add another piece to the puzzle.

    I do believe that perl interally treats a qr// as a blessed scalar, being blessed into class Regexp. The object itself is as magical as objects get I think, and we can't get any useful info out of just printing the object as you usually can, but overload can help us out:
    use overload; print overload::StrVal(qr/asdf/); # Regexp=SCALAR(...)
    And Devel::Peek help us in another way:
    use Devel::Peek; Dump(qr/asdf/);
    yielding:
    SV = RV(0x1af1fd8) at 0x1b0f118 REFCNT = 1 FLAGS = (PADBUSY,PADMY,ROK) RV = 0x1b0f190 SV = PVMG(0x1c2c5ac) at 0x1b0f190 REFCNT = 1 FLAGS = (OBJECT,RMG) IV = 0 NV = 0 PV = 0 MAGIC = 0x1af012c MG_VIRTUAL = 0x2808c138 MG_TYPE = 'r' MG_OBJ = 0x1b0b51c STASH = 0x1de7570 "Regexp"
    showing us that regexes are magical. I don't see it being defined anywhere. But I have no idea why this is the case. But I can't say I'd expect a sane dereference value for a magical object. :) In fact, thinking about it I'd more expect undef than anything else because... the value isn't defined since it's not actually a scalar, and what would be a good substitute value anyway?

    Hoped this helped in some way,
    ihb
Re: Undefined regexp objects?
by Paladin (Vicar) on Jan 15, 2003 at 23:39 UTC
    qr// doesn't return a reference. Try:
    use strict; use warnings; my $r = qr/Delectable/; print $r;
    Update: ihb is quite right. I should have qualified that as scalar reference.
      qr// does indeed return a reference, try
      print ref(qr//)
      This object stringifies through, so you can interpolate it into other patterns.

      ihb

        In fact... you can also call methods off of it and rebless the object into a different package and it keeps it's Regexp nature.

        use strict; use warnings; my $r = qr/test/; print $r -> match( 'test' ) ? "Match\n" : "No match\n"; bless $r, 'beans'; print $r -> match( 'test' ) ? "Match\n" : "No match\n"; package beans; sub match { $_[1] =~ $_[0] } package Regexp; sub match { $_[1] =~ $_[0] }
Re: Undefined regexp objects?
by pg (Canon) on Jan 16, 2003 at 05:35 UTC
    The comments explains what this piece of code is doing:
    use B; $foo = qr/foo/; if ($foo->isa("Regexp")) {#check whether it is a Regexp object print "is a Regexp\n"; } else { print "is not a Regexp\n"; } print B::svref_2object($foo)->MAGIC->TYPE;#if it is not a sv ref, perl + will not allow you to call svref_2object