Practical Perl wrote:
>
> I got a warning message from my perl script,it's this:
>
> Quantifier follows nothing in regex; marked by <-- HERE in m/? <-- HERE 
?????/ at ./xxxx.pl line 703.
>
> The 703st line is in a subroutine,the subroutine is:
>
>
> sub some_sub
> {
>    my $arg1=shift;
>    my $ok=0;
>
>    for (@some_global_array)
>    {
>        return ($ok =1) if $arg1=~/$_/;

There's no point in assigning to $ok here, by the way. Just

  return 1 if $arg1=~/$_/;

is fine.

>    }
>
>    return $ok;

And $ok is always equal to zero here, so there's no need for it at all:

  return 0;

does the trick.

> }
>
>
>
> What this mean?thank you.

Hello Practical

Your $_ variable contains '??????'. Look at this minimal Perl program:

  /??????/;

OUTPUT:

  Quantifier follows nothing in regex; marked by <-- HERE in m/? <-- HERE 
?????/ at ./x.pl line 1.


The question mark is a regex 'quantifier', and means that whatever precedes it
may or may not appear in the matched string. Because nothing precedes the first
question mark Perl is telling you that it's an illegal regex.

If you want to match a literal '?' then you need to escape it:

  /\?\?\?\?\?\?/;

to escape all metacharacters in a string, use quotemeta().

If, as Tom says, you want to test for string equality, use 'eq':

  return 1 if $arg1 eq $_;

and finally, if you want to check a value against all elements of
a list, use grep():

  sub some_sub {
    my $arg1 = shift;
    scalar grep($arg1 =~ /$_/, @some_global_array);
 }

which will return the number of matching array elements rather than 1 or 0, but
this still suffices as a logical true value.

HTH,

Rob

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to