开发者

why can't I use this in my subroutine?

I have the following:

$sql="SELECT * FROM table WHERE name = ? ";
&checkDB( $sql , "bob" , "while"  );

sub checkDB(){

my $sth=$dbh->prepare($_[0]) or warn "$DBI::errstr";
$sth->execute( $_[1] ) or warn "$DBI::errstr";

print $_[2] . "\n"; # this works
    $_[2] ( my @rows= $sth -> fetchrow() ) { # this doesn't work
    blah, blah, blah
    }


}

I pass my sql statement, 'bob' and either a "while" or "unless" variable into the subroutine. My subroutine will let me pass the "while" variable (it will print "while\n") but it won't let me use it to fetchrows. What am I doing wrong?

my exact error is "syntax error at script.pl near "}" "....works fine if I substitute $_[2] with the actual word "whil开发者_高级运维e"


Because while and "while" are different things.

You can't substitute arbitrary language constructs for strings.


It is not possible to use variable in place of keyword in perl. If you really must do such constructs, look at eval - it lets you run arbitrary string as perl code.


I think what you would need here is an eval, however this has evil written all over it. You definitely want to be sure that nothing in that code is tainted (coming from user). I think you probably should think very hard about why you would want to do this and if there isn't a better way (two different functions for example). I know we programmers like to be lazy, but evals are dangerous.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜