Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: dynamic perl code inside my perl program

by flounder99 (Friar)
on Oct 20, 2003 at 19:49 UTC ( [id://300727]=note: print w/replies, xml ) Need Help??


in reply to dynamic perl code inside my perl program

Remember that if you eval $szBlock it will return a reference to the subroutine. You will then have to call that subroutine reference.

This looks like homework so I shouldn't be giving you the answer but I don't know of any better way to explain it.

use strict; my $szBlock = q( sub { if ( $x > $y ) { return "X greater \n" ; } else { return "y greater \n" ; } } ) ; # # $szBlock is a string containing the text of the subroutine. # my ($x, $y) = (100,200); # # Set up $x and $y (you will have to do this before the eval # or strict won't like it.) # my $subref = eval $szBlock; # # if the eval worked $subref now has a reference to the # compiled subroutine # die $@ if $@; # # $@ will be true if the eval failed (if there was a typo in $szBlock) # # # Now we call the subroutine by "dereferencing" the reference in $subr +ef # print &$subref;
This will print
y greater

--

flounder

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-04-18 09:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found