user11674007
user11674007

Reputation:

i want to search for a string in a file using perl

i want to search for a string in file using perl code

i want to search "unix" string from sample input file

use strict;
use warnings;
my $file = "sample";
open my $fh, '<', $file or die "Could not open '$file' $!\n";

while (my $line = <$fh>) {
   chomp $line;
   my @strings = $line = ~ /unix/g;
   foreach my $s (@strings) {
     print "'$s'";
   }
}

i am getting like this

Use of uninitialized value $_ in pattern match (m//) at strings.pl line 8, line 1. '18446744073709551615'

Upvotes: 0

Views: 146

Answers (1)

Dave Cross
Dave Cross

Reputation: 69244

As people have already mentioned in comments, you have a small typo. The binding operator, =~, cannot have a space between its two characters.

You might expect that adding that unwanted space would generate a syntax error. But, unfortunately, both = and ~ are used as operators so instead of a syntax error, you get a statement that does something completely different to what you expect.

Upvotes: 2

Related Questions