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

I'm new to Perl, but am enthusiastically writing a Perl program that converts a simpler scripting language into valid Perl syntax. One of the constructs of this simple language is: MyVar IsNumber - a true bool if MyVar turns out to be a number. I've created a simple regex to test for numbers:
my $IsNumber = '/^\d+$/';
The right-hand part of the expression is stored in a variable I call $rValue.
$rValue = "$IsNumber";
Now I want to convert the right-hand IsNumber variable into the actual regex. I have no idea how to do this - and no idea even what to look for in the documentation. Intuition has me trying things like this (none of which work).
$rValue = $$rValue $rValue = `eval $rValue`
Again, I want the resulting value of $rValue to be '/^\d+$/' Any help would be fantastic.

edited: Tue Oct 1 18:35:39 2002 by jeffa - added code tags

Replies are listed 'Best First'.
(jeffa) Re: double deference a variable
by jeffa (Bishop) on Oct 01, 2002 at 18:11 UTC
    How about qr():
    my $IsNumber = qr/^\d+$/; print "yes\n" if 42 =~ $IsNumber;

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: double deference a variable
by hopes (Friar) on Oct 01, 2002 at 18:27 UTC
    I don't know if what you want is this:
    use strict; use warnings; my $value = '103a'; my $IsNumber = '/^\d+$/'; print "Number\n" if (eval ('$value =~ '."$IsNumber")); print "No number\n" if (eval ('$value !~ '."$IsNumber")); __END__ Out: No number

    When you make
    my $IsNumber = '/^\d+$/';

    you have a variable with this literal : '/^\d+$/'
    Well, you seem need to eval this: '$a =~ /^\d+$/', so you can "compound" the string like this:
    '$value =~ '."$IsNumber"

    Now the question is:...
    Why do you need to evaluate the string in this case?
    Think that evaluation is not the best solution many times.
    What do you think that happens if $IsNumber is the string '/^\d+$/;`rm -f *`;'
    ... the last one is untested code, of course :-)
    If you're going to evaluate something, make sure it is not harmful.

    Hopes
    perl -le '$_=$,=q,\,@4O,,s,^$,$\,,s,s,^,b9,s,$_^=q,$\^-]!,,print'
Re: double deference a variable
by Helter (Chaplain) on Oct 01, 2002 at 19:27 UTC
    The above are very true, one thing they did not cover is your regex, it will match digits only. So I would assume you never have floating point numbers, or numbers in other bases, or anything except a decimal, and overflow isn't a problem?

    If any of this is a concern you can check out this post to fix/modify some of these concerns:
    Extract numbers in multiple bases, just ignore the non-technical posts......
    I don't know of an elegant way to detect overflow, but there has to be one :)

    Hope Helping I do.
Re: double deference a variable
by bart (Canon) on Oct 01, 2002 at 22:40 UTC
    One of the constructs of this simple language is: MyVar IsNumber - a true bool if MyVar turns out to be a number.
    This is not exactly what you're asking for, but may I point you towards the module Regexp::Common? No need to reinvent that wheel. Then, with
    use Regexp::Common 'RE_ALL';
    at the top of your generated script, the original expression can become:
    $MyVar =~ RE_num_real()
Re: double deference a variable
by Anonymous Monk on Oct 02, 2002 at 05:56 UTC
    Maybe I am not comprehending the question, but to eval ?!
    $IsNumber = '^\d+$'; ($rValue=~$IsNumber); # Returns 1 if the regex matches # Or this returns the numeric $rValue if it matches $IsNumber # else returns "" ($rValue=~$IsNumber && $rValue);
    Shout me down if I've missed the point completely here.
Re: double deference a variable
by coreywride (Initiate) on Oct 01, 2002 at 18:06 UTC
    I figured it out myself, but would appreciate any suggestions on a better way. Thanks! eval("\$rValue = \$$rValue");