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

for(param()) { push (@num, $_) if $_ ~= m/^d$+/ }
Produces syntax error at admin.pl line 98, near "$_ ~". Can someone help me fix it?

Replies are listed 'Best First'.
Re: syntax error
by FunkyMonk (Bishop) on Aug 19, 2007 at 17:52 UTC
    It should be =~ (= and then ~).

      = and then ~
      but don't put any whitespace between the two characters, because that's valid too, and means something entirely different! = is plain assignment and ~ is the unary bitwise not operator. So it would mean:
      $_ is assigned the value of bitwise not the value of (whatever expression, in this case, how many times did the match succeed?)
        Ahhggg. I was trying to help the OP avoid an easily missed mistake, that's why I spelled it out.

        You're quite right, of course.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: syntax error
by kyle (Abbot) on Aug 19, 2007 at 18:10 UTC

    Try this (change "~=" to "=~"):

    for(param()) { push (@num, $_) if $_ =~ m/^d$+/ }
Re: syntax error
by artist (Parson) on Aug 19, 2007 at 23:32 UTC
    for(param()) { push (@num, $_) if $_ =~ m/^\d$+/ }
    You also want to make sure, that it's \d rather than d in the match expression.
    --Artist
Re: syntax error
by pemungkah (Priest) on Aug 20, 2007 at 07:17 UTC
    for(param()) { push @num, $_ if m/^d$+/ } will also work -that is, removing $_ and the erroneous operator altogether - since the default target for a pattern match operation is $_.

    Some will consider this clearer because shorter; others will feel that implied variables in an expression are insufficiently clear. I lean toward the former, myself. It's a matter of personal taste - and whether you'd have to explain it.