Reputation: 3928
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 "while"
Upvotes: 0
Views: 100
Reputation: 944474
Because while
and "while"
are different things.
You can't substitute arbitrary language constructs for strings.
Upvotes: 2
Reputation: 20280
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.
Upvotes: 1