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

Hi to all !

when reading doc by "perldoc -f ref" I get, this sentence :

The result "Regexp" indicates that the argument is a re +gular expression resulting from "qr//".
So, for testing purpose, I tried this code :
#!/usr/bin/perl my $regX = qx /\.pm/ ; printf ("type of regX=%s\n", ref(\$regX));

And I get "type of regX=SCALAR" instead of the waited "Regexp".

What did I do wrong ??

Some infos:

Perl 5.12.3 from ActiveState

I tried {/\.pm/} with no more success.

Thanks to all !

Replies are listed 'Best First'.
Re: ref and qx
by ikegami (Patriarch) on May 06, 2011 at 05:40 UTC

    First, you're using qx (aka readpipe) instead of qr.

    Second, \$refX is a reference to scalar $refX, and thus ref(\$refX) returns "SCALAR". If the value of $refX had been constructed using qr, ref($refX) would get you "Regex" since it contains a reference to a Regex.

    I recommend that you use re::is_regexp instead if you're using Perl 5.10+. This will catch both references to regex patterns and a regex patterns themselves.

    >perl -E"$r=qr/a/; say ref($r) eq 'Regexp' ||0" 1 >perl -E"$r=qr/a/; say ref($$r) eq 'Regexp' ||0" 0 >perl -E"$r=qr/a/; say re::is_regexp($r) ||0" 1 >perl -E"$r=qr/a/; say re::is_regexp($$r) ||0" 1
      Shame on me !!!!

      And many thanks to you !