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

I would like to invoke the builtin "uc" using a reference, however the following does not work (I suspect I'm not using the correct namespace for builtin functions):
my(%hash) = ( "sub" => \&uc, ); print $hash{"sub"}->("hello");
I do NOT want to redefine "uc", just "refer" to it. I've tried UNIVERSAL::uc, CORE::uc and CORE::GLOBAL::uc but they all complain with something like (depending on the namespace I try to use):
Undefined subroutine &CORE::uc called at ./test.pl line 20.
under 5.005_03
-Adi

Replies are listed 'Best First'.
Re: How do I construct a reference to a builtin function?
by archon (Monk) on Mar 21, 2001 at 05:04 UTC
    I don't think you can construct a reference to a builtin. References are to things with memory addresses and builtins don't have any (if i am wrong, someone will yell at me).

    This will more or less accomplish what you want to do, though:

    my %hash = ( foo => sub {uc shift}, ); print $hash{foo}->("hello\n");
    HTH, HAND.
Re: How do I construct a reference to a builtin function?
by ColonelPanic (Friar) on Mar 21, 2001 at 08:44 UTC
    This seems like an odd thing to do. Just out of curiosity, why do you want to do it?

    When's the last time you used duct tape on a duct? --Larry Wall
      I'm validating input to a function, via named parameters, that inserts stuff into a database using DBI. For each parameter passed (say %param), I run it through a bunch of validation routines, massage it and spit out what, if all tests pass, should be inserted into the database...my validation routines for each parameter might be different and are stored as references in a hash. ie.
      %validationHash = ( "name" => { "regexp" => "string", "status" => "required", "outputsub" => \&massage; }, "phone" => { "regexp" => "phone", "status" => "optional", "outputsub" => \&mungephonenum, }, );
      then I iterate over the keys in the hash (ie.$param{'name'}) (thereby ignoring any parameters passed that I don't care about), make sure it is defined if $validationHash{"name"}{"status"} eq "required" otherwise return undef etc. then make sure that it looks like a "string" with a routine that runs a regexp match and finally, return a value $validationHash{'name'}{'outputsub'}->($param{'name'}) as what should be inserted into the database.

      Needless to say, I would love to hear about nice ways of validating input and even better, ways of constructing SQL statements given such input (I'm considering using DBIx::Recordset for the latter, but I've done enough work already that I might have to come back to it at another time).

      BTW, the first solution given worked very well and I am using it successfully, thanks.
      -Adi