You can either use $item = quotemeta "*varName" or later match against /\Q$item\E/ | [reply] [d/l] [select] |
If you always want $item to be treated like a literal string with no regex special characters, use the index function instead of a regex match:
my $func_def = "char *strcmp(const char *s1, const char *s2)";
foreach my $i ('char *','const char.*','*')
{
if (index($func_def,$i))
{
print "'$func_def' contains '$i'\n";
}
}
Update: ishnid is right; I didn't read the index documentation closely enough. Use his code instead. :) Thanks ishnid!
| [reply] [d/l] [select] |
Though I'd agree that index could be used, testing index's return value for truth is only useful in some circumstances. If $i isn't contained in $func_def, index returns -1, which is a true value, so your code will think it's been found. Similarly, if $i is at the start of the string (as the first example in yours is), index returns 0 (its position in the string), which is a false value.
In order to test if one string contains the other, you need to check that index returns >= 0.
Amended Code:
my $func_def = "char *strcmp(const char *s1, const char *s2)";
foreach my $i ('char *','const char.*','*')
{
if ( index( $func_def, $i ) >= 0 )
{
print "'$func_def' contains '$i'\n";
}
}
| [reply] [d/l] [select] |
| [reply] [d/l] |
"regex expression"? Is that anything like an "ATM machine" or a "PIN number" (or a "rice paddy")? :)
| [reply] |