subroutine - Perl sub made from string -
i trying use sub in perl contained in string.
currently have
$sub = "sub{ $in = shift; if($in =~ /bla.*wam/){return 1;}}"; i try , use doing
$sub->("test"); or
&{$sub}->("test"); both examples above spit out whole function if name of sub not find. looks this:
undefined subroutine [function printed here] what getting wrong here?
let's scalar $sub contained string "foobar". if $sub->(), attempting call subroutine named foobar. if subroutine not exist, error.
you trying call subroutine name sub{ $in = shift; if($in =~ /bla.*wam/){return 1;}}, thoroughly ridiculous name sub , doesn't exist in program. (and actually, since it's double-quoted, $in interpolated without realizing it.)
so, first of all, don't that.
if want anonymous subroutine, make this:
my $sub = sub { $in = shift; if($in =~ /bla.*wam/) { return 1; } }; then execute this: $sub->("test");
if really need execute code in string, can use eval.
my $sub = eval 'sub{ $in = shift; if($in =~ /bla.*wam/) { return 1; } }'; that evaluate code in string , return result, sub reference. careful these strings coming from. whoever makes them can make program whatever want.
Comments
Post a Comment