in reply to dynamic perl code inside my perl program
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.
This will printuse 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;
y greater
--
flounder
|
|---|