Q: Adding Optional option to expression match

M

Martin Daser

Hello *,
I am currently writing a small script to search various patterns within
files.
Therefore I am looping over a bunch of strings and try to find the
pattern. Optionally I want to set a variable to certain flags, which can
be appended to the matching operator. The variable is either empty or
contains some switches/characters.
Adding the variable which holds the switches results in a syntax error:

Scalar found where operator expected at /...../rgrep line 88, near
"m/($word)/$matchopts"
syntax error at /...../rgrep line 88, near "m/($word)/$matchopts"
syntax error at /...../rgrep line 96, near "}"
Execution of /...../rgrep aborted due to compilation errors.

Here is the excerpt to give you an idea:

# set option for ignore case matching; others possible
$matchopts = "i";
.....

# the next line would be line 88
if ($_ =~ m/($word)/$matchopts)
{
.....
}

Is there a way to avoid this error? How can I get around it?

Any help is appreciated! Thanks in advance!

-- Martin
 
B

Brian McCauley

Martin said:
Scalar found where operator expected at /...../rgrep line 88, near
"m/($word)/$matchopts"
syntax error at /...../rgrep line 88, near "m/($word)/$matchopts"
syntax error at /...../rgrep line 96, near "}"
Execution of /...../rgrep aborted due to compilation errors.

Here is the excerpt to give you an idea:

# set option for ignore case matching; others possible
$matchopts = "i";
....

# the next line would be line 88
if ($_ =~ m/($word)/$matchopts)
{
....
}

Is there a way to avoid this error? How can I get around it?

m/((?$matchopts)$word)/

BTW
Why do you say "don't use $_, use $_ instead"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top