Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

How do I get a reference to a builtin function?

by adram (Initiate)
on Mar 21, 2001 at 04:44 UTC ( [id://65922]=perlquestion: print w/replies, xml ) Need Help??

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

I would like to call a built-in function (in this case, uc) via a reference.
However, the following does not work.
(I suspect I'm not using the correct namespace for built-in functions.)

my %op = ( 'toupper' => \&uc, ... ); print $op{'toupper'}->($text);
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 ...

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I get a reference to a builtin function?
by dws (Chancellor) on Mar 21, 2001 at 04:51 UTC

    I don't know of a way to do that. But perhaps you'd find the following solution acceptable: writing your own wrapper functions to which you can refer.

    my %op = ( 'toupper' => sub { uc shift }, ... );

Re: How do I construct a reference to a builtin function?
by jlawrenc (Scribe) on Mar 21, 2001 at 05:08 UTC
    I do not know, personally, of any way to reference an builtin function. Although your idea has merit it may not be possible. Those more familiar with perlguts that I may have a different answer.

    Now, if this specific magic is on your mind I will be of no help, but I offer this to try and satisfy your builtin reference cravings:

    my(%hash) = ( "sub" => sub { uc $_[0] }, ); print $hash{"sub"}->("hello");
    Since uc expects only a scalar we'll have to pick the first element of @_ for it. You may elect to use join as means to handle arrays in your call to "sub".

    Your referencing strategy will differ slightly from function to function depending on what you want to accomplish.

    Originally posted as a Categorized Answer.

Re: How do I get a reference to a builtin function?
by ikegami (Patriarch) on Jun 15, 2009 at 15:28 UTC
    Really? It shouldn't.
    use strict; use warnings; my %op = ( 'toupper' => \*uc{CODE}, #... ); print( $op{toupper}->('foo'), "\n" );
    Name "main::uc" used only once: possible typo at a.pl line 5. Not a CODE reference at a.pl line 10.

    Originally posted as a Categorized Answer.

Re: How do I get a reference to a builtin function?
by erebusx (Initiate) on Jun 15, 2009 at 15:19 UTC
    try this
    my %op = ( 'toupper' => \*uc{CODE}, ... );
    at least, this works for me^_^

    Originally posted as a Categorized Answer.

Re: How do I get a reference to a builtin function?
by igoryonya (Pilgrim) on Dec 08, 2009 at 10:07 UTC
    I'd say:
    my %op = ( 'toupper'=>sub{uc(@_)}, ... );
    instead of shift
      Please test your code before you offer it as a solution:
      $ perl -wE 'my $t = sub { uc(@_)}; say $t->("foo")' 1

      uc has a prototype which forces scalar context, so uc(@_) evaluates to the number of elements in @ (upper-cased).

      Perl 6 - links to (nearly) everything that is Perl 6.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://65922]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-03-28 17:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found