in reply to Passing variable to if statement

The problem is with this line:

if (/$name/ismo) {

Remove the /o ("compile only once") modifier to your regex, and it'll work just fine.

Here's what Regexp Quote Like Operators has to say on /o:

PATTERN may contain variables, which will be interpolated every time the pattern search is evaluated, except for when the delimiter is a single quote. (Note that $( , $) , and $| are not interpolated because they look like end-of-string tests.) Perl will not recompile the pattern unless an interpolated variable that it contains changes. You can force Perl to skip the test and never recompile by adding a /o (which stands for "once") after the trailing delimiter. Once upon a time, Perl would recompile regular expressions unnecessarily, and this modifier was useful to tell it not to do so, in the interests of speed. But now, the only reasons to use /o are either:

  1. The variables are thousands of characters long and you know that they don't change, and you need to wring out the last little bit of speed by having Perl skip testing for that. (There is a maintenance penalty for doing this, as mentioning /o constitutes a promise that you won't change the variables in the pattern. If you do change them, Perl won't even notice.)

  2. you want the pattern to use the initial values of the variables regardless of whether they change or not. (But there are saner ways of accomplishing this than using /o.)

  3. If the pattern contains embedded code, such as

    use re 'eval'; $code = 'foo(?{ $x })'; /$code/

    then perl will recompile each time, even though the pattern string hasn't changed, to ensure that the current value of $x is seen each time. Use /o if you want to avoid this.

The bottom line is that using /o is almost never a good idea.

Emphasis mine.

Replies are listed 'Best First'.
Re^2: Passing variable to if statement
by revhippie (Scribe) on Jul 16, 2014 at 23:53 UTC
    For that matter, remove the /sm, too. They're pointless here.