in reply to Can Perl add single quotes?
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.
|
|---|