in reply to perl if statement

How do you know that:
if (grep /($FORM{user}|$FORM{pin})/i, "/export/install/users.txt") {
evaluates to true? This is very unlikely.

Take a look at what the grep function does, it is not what you apparently think. In Perl, grep is used to filter the elements of an array.

Replies are listed 'Best First'.
Re^2: perl if statement
by Drsin (Novice) on Feb 29, 2016 at 20:06 UTC
    Hi Laurent, You are right.. I am thinking more of shell script grep. what ways can I get this done in Perl.. any help appreciated.
      Well, for changing as little as possible to your code, you could slurp the content of the file into an array and then use your grep function on that array.

      One quick and simple possibility is as follows:

      my $infile = "/export/install/users.txt"; open my $IN, "<", $infile or die "Cannot open $infile $!"; my @lines = <$IN>; if (grep /($FORM{user}|$FORM{pin})/i, @lines) { # ... }
        Hi Laurent, Many thanks, it worked. The only issue is that the grep:  grep /($FORM{user}|$FORM{pin})/i only matches the first condition even when both conditions are on the same line.