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

I would like to do a reality check on lots of variables and not have to write a seperate function for each one. Is there a way to do this?
reality_test($test_me, $min_allowed, $max_allowed) sub reality_test { #test here #if the test fails print '$test_me was less than allowed'; }
Note the single quotes in the print statement.
I guess my question boils down to: Is there a way to get Perl to put a $variable inside of single quotes so that I can store that name in a variable that I can pass to the sub.

Replies are listed 'Best First'.
(tye)Re: Can Perl add single quotes?
by tye (Sage) on May 18, 2001 at 09:33 UTC
    reality_test( $test_me, '$test_me', $min, $max );

    Not really what you wanted, eh? But, I recommend it because you really want to write:

    isInRange( $cntFiles, 'Number of Files', $minFiles, $maxFiles );
    because print sends stuff to the person using your script, not to the person writing your script. The person using your script shouldn't need to know what names you gave to your variables.

    Something more like what you want would be:

    isInRange( *test_me, $min, $max ); sub isInRange { my( $glob, $min, $max )= @_; my $val= $$glob; my $name= substr($glob,1); if( $val < $min ) { die "\$$name is less than $min.\n" } elsif( $max < $val ) { die "\$$name is more than $max.\n" } else { return 1; } return !1; }
    but that doesn't work with lexical variables (which are the types of variables you should use 95% of the time) and doesn't work under use strict (which is what you want to use 95% of the time). So "just say 'no'".

    Then there is the C preprocessor option... Actually, it isn't really much of an option. Nevermind. Trust me; you don't even want to know.

            - tye (but my friends call me "Tye")
Re: Can Perl add single quotes?
by grinder (Bishop) on May 18, 2001 at 13:26 UTC

    You cannot pass a variable to a function (whether it be by value or by reference) and know its name at the same time. You can, however, pass a string that represents the name of the variable, and then evaluate the string to get its value.

    #! perl -w use strict; use vars qw/$foo/; $foo = 3; my $bar = 4; sub reality_test { my ( $var_name , $bound ) = @_; my $var = eval $var_name; die "$var_name is not syntactically correct: $@\n" if $@; print "'$var_name' was less than allowed\n" if $var < $bound; } reality_test( '$foo' , 10 ); reality_test( '$bar' , 10 );

    That's a sort of a weird thing to do, but hey, what ever gets you through the night...

    The trouble is you have to use the non-standard convention of passing the variable name in a string. You may remember to do this, but you will not endear yourself to your coworkers. If you forget to pass a string, and pass a variable instead, it still works correctly, but the script no longer prints out the name of the variable but rather its value.


    --
    g r i n d e r
Re: Can Perl add single quotes?
by yakko (Friar) on May 18, 2001 at 09:17 UTC
    how about:
    sub reality_test { do { my $testme=shift; #do tests print "$testme was less than allowed\n" if test fails; } while(@_); }
    That way, you don't use globals to pass to your subs, and you get variable interpolation, as '$var in single quotes' doesn't get expanded.

    (argh, Opera managed to munge that a bit. corrected)

    --
    Me spell chucker work grate. Knead grandma chicken.

Re: Can Perl add single quotes?
by Gloom (Monk) on May 18, 2001 at 11:39 UTC
    You want check a variables by its name ? thus try something like this :
    sub reality_test { my ( $var_name , $min , $max ) = @_; eval "print q($var_name is out of range !) if $var_name < $min or +$var_name > $max" } reality_test( '$foo' , 10 , 30 );
    But is not really "programmatically correct" =)
    __________________
    Hope this helps
Re: Can Perl add single quotes?
by busunsl (Vicar) on May 18, 2001 at 10:13 UTC
    I you want to put just the variable between single quotes, use this:
    print "'$test_me' was less than allowed";