steph_bow has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I have a variable which I would like to use for character recognition but it does not seem to work. Could you check ? Thanks

my $song = "cool"; if (my $line = /score*"$song"/){ ... }

Replies are listed 'Best First'.
Re: character recognition
by hipowls (Curate) on Mar 06, 2008 at 12:28 UTC

    Your regular expression is probably not doing what you think it is. It matches a line that has a literal 'scor', zero or 'e's then a literal '"cool"'. You probably mean to use '.*' to match zero or more characters (except newline).

    if ( my $line =~ /score.*$song/ ) { ... }
    but without seeing the input you expect to match I can't be sure.

Re: character recognition
by moritz (Cardinal) on Mar 06, 2008 at 12:28 UTC
    Describe what you want to match, then we can help you.

    Your regex will match scor"cool", score"cool", scoree"cool ", scoreee"cool" etc.

    Maybe you meant m/score.*$song/?

    BTW I recommend perlretut as an introduction, or "Mastering Regular Rexpressions" if you'd rather like a book.

Re: character recognition
by olus (Curate) on Mar 06, 2008 at 12:45 UTC

    Besides what has already been said, you should notice that you are not testing for a match of $line to the regexp. You assigning to $line the result of the match of $_, if already defined, to the regexp. Maybe a typo, but you should use =~.