Perl - Grep function regular expression

For some reason,
@logs is a list of log files
@filter is a list of expressions to grep out

foreach (@logs){
open READ, "<$_" or die $!;
@temp=<READ>;
close READ;

    foreach (@filter){
        print grep /$_/,@temp ;    
        
}
}

returns a regex error in one of the files it's parsing.

while

print grep $_,@temp ;

returns the entire file

while

print grep /string/,@temp ;

works fine.

I can not seem to wrap my head around it today. Any clues?

what u wanna get actualy?
you just opening every file and reading it then trying to grep some filter-shit from it's content..
is it what u want?
i'm not sure if grep can work for the whole array..
why not to do it when u actualy opened the file and while($line=<FILE>){grep it} ?
anyway try to use $somevar, not $_

Try to use the list context , ex (not tested):

foreach (@logs){
open READ, "<$_" or die $!;
@temp=<READ>;
close READ;

    foreach $var (@filter){
          @u=(@u,grep /$var/,@temp );    
        
}
}

foreach $u (@u){ print "u:".$u."\n"; }

Everything you have checked is right.. Understand as mentioned below.

When the file has a line with just single open paranthesis, then you will get regex error ?! What is the big deal in this ?

If you want to avoid then escape it using either quotemeta function, or use \Q. Using some input directly as regex is not good, as you have this risk.

What does grep will do ? If for current iteration the return value is true then the current $_ will be pushed in to the result array, so you get the entire array, as each time you have some value at $_ and that returns true.

If you dont understand the basics, you will end up in confusions.

It would exit the program at this point.

I ended up having to go with passing $_ to a scalar variable and then placing the scalar variable into an array.

foreach (@logs){
open READ, "<$_" or die $!;
@temp=<READ>;
close READ;
    foreach (@filter){
        my $alt=$_;
        print grep {/\Q$alt\E/i} @temp ;    
        }
}

Thank you all for your help.

This is why I'm here. I was able to figure out the problem after I thought about what was happening for awhile and researched how the grep() function operated. And remembered about Quotemetas.

Just out of curiosity, when I do the following:

foreach (@logs){
open READ, "<$_" or die $!;
#@temp=<READ>;
        foreach (@filter){
        my $alt=$_;
        print grep {/\Q$alt\E/i} <READ>;    
        }
close READ; 
}

It seems not to go through the @filter array, but rather only the first value of it. Why would this be when trying to read directly from the file?