in reply to How to read the file from stdin using perl?
Hello gopikavi,
I am bit confused as the rest of the monks.
Do you mean that you want something like that? Reading STDIN and if exists inside your file while print the whole file?
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; $| = 1; # turn on autoflush my $file = 'file.txt'; open my $fh, "<", $file or die "Could not open '$file': $!"; chomp(my @files = <$fh>); close $fh or die "Coould not close '$file' $!"; while (my $stdin = <>) { chomp $stdin; last if $stdin eq ""; if ( grep { $stdin eq $_ } @files ) { print Dumper \@files; } else { print "There is no word in the $file\n"; } } __DATA__ $ perl file.pl data1 $VAR1 = [ 'data1', 'data2', 'data3', 'data4' ]; DATA1 There is no word in the file.txt
Update: Adding last if $stdin eq ""; to exit loop.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to read the file from stdin using perl?
by gopikavi (Initiate) on May 09, 2017 at 11:06 UTC | |
by thanos1983 (Parson) on May 10, 2017 at 09:12 UTC |