Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Coderef from scalar.

by vaevictus (Pilgrim)
on Dec 29, 2001 at 04:51 UTC ( #135025=perlquestion: print w/replies, xml ) Need Help??

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

Security issues aside, I want to be able to pass perlcode in a scalar, to a function that takes coderefs only. (Net::IRC::Connection->add_handler)

my $code='{ print "hello, perlmonks\n"; }'; sub samething { print "hello, perlmonks\n"; } &specialfunction( \&samething ); #normal &specialfunction( { print "hello, perlmonks\n"; } ); #also valid &specialfunction( $code ); #not a coderef!

How do i make this last line work?

Replies are listed 'Best First'.
(Ovid) Re: Coderef from scalar.
by Ovid (Cardinal) on Dec 29, 2001 at 05:01 UTC

    I wouldn't use an eval, if it's not necessary.

    perl -e '$code = sub{ print "Ovid is a blowhard\n" };&$code'

    This is better because your code is checked at compile time and you don't reinvoke the Perl interpreter.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      Defined as the problem, I *must* use a scalar full of code.

        This is often true, so I can understand why an eval might be necessary. What, exactly, is the source of the scalar? Is this being built at runtime? If so, eval might be the answer. If it's a bunch of static code snippets that can be determined at compile time, a hash of coderefs is faster and safer.

        Cheers,
        Ovid

        Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Coderef from scalar.
by VSarkiss (Monsignor) on Dec 29, 2001 at 05:04 UTC

    Contrary to the above two answers, you don't need eval to build a code ref. Just an anonymous sub will do it:

    my $code = sub { print "hello perlmonks\n"; }; # &specialfunction($code);
    More can be found in perlman:perlref. Look at the section titled "Making references".

    HTH

      $code *must* start out as a string of characters that are legal perlcode.
Re: Coderef from scalar.
by larryk (Friar) on Dec 29, 2001 at 04:55 UTC
    specialfunction( sub { eval $code } );
       larryk                                          
    perl -le "s,,reverse killer,e,y,rifle,lycra,,print"
    
      This works... now i just have to keep it secure. :) Thanks.
Re: Coderef from scalar.
by Juerd (Abbot) on Dec 29, 2001 at 04:58 UTC
    You use eval to return a coderef.

    $code = 'print "Hello, World!\n"'; $coderef = eval "sub { $code }"; $coderef->();

    2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2022-01-26 12:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (69 votes). Check out past polls.

    Notices?