gopikavi has asked for the wisdom of the Perl Monks concerning the following question:

Here am reading the file directly but my query is how to readd the file with the standard input word given.
open F, "<file.txt" or die $!; while (<F>) { print $_; } close F;
File.txt
data1 data2 data3 data4

How should my output works which as follows
Enter the name:data1(Here i give data1 the word is present in the file.txt).So now it should read entire file.txt
Incase if i give data5 as standard input there is no word in the file.txt then the print statement should show name is not present in the file.txt.
Now my query is i dont know how to give standard input and from standard input how to read the file.
How can i do it?.

Replies are listed 'Best First'.
Re: How to read the file from stdin using perl?
by shmem (Chancellor) on May 09, 2017 at 08:59 UTC

    The file handle associated with standard input is STDIN. You need not open it. You can read from this filehandle as you do with your filehandle F.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
      are you suggesting me to pass STDIN instead of filename.

        No. I am suggesting to read the filehandle STDIN as you do with your filehandle F - via the diamond operator "<>" or readline. No need to pass STDIN anywhere.

        $| = 1; # turn on autoflush print "Enter file name: "; my $answer = <STDIN>; chomp $answer; # remove newline char from answer print "file name is '$answer'\n";
        perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re: How to read the file from stdin using perl?
by thanos1983 (Parson) on May 09, 2017 at 10:34 UTC

    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.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
      Here you had given as data1 then why it goes for else condition

        Hello again gopikavi,

        Sorry for the late reply, I got busy with something that I was working on.

        Back to your question, Here you had given as data1 then why it goes for else condition. Notice the difference between DATA1 and data1. The search that I am conducting is case sensitive. Which means if you want to be able to search for small case characters and the same time big case characters you need to convert the user input to small case characters with lc. But this is entirely up to you, it depends what you want to do and how flexible or strict the script should be.

        For example:

        #!/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 ""; # exit stdin if empty string if ( grep { lc $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 $VAR1 = [ 'data1', 'data2', 'data3', 'data4' ]; DATA100 There is no word in the file.txt

        Hope this helps.

        Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: How to read the file from stdin using perl?
by NetWallah (Canon) on May 09, 2017 at 16:59 UTC
    I think the OP is looking for something like this:
    use strict; use warnings; my $input_file='file.txt'; while (1){ # Infinite loop print "Enter the word you are looking for (or 'quit' to exit): "; my $answer = <STDIN>; chomp $answer; # remove newline char from answer last if $answer =~/quit/i; # Exit loop print "Looking for '$answer'\n"; my $found = 0; open my $f, "<", $input_file or die "ERROR: Cannot open $input_file + : $!"; while (<$f>) { m/$answer/ or next; $found=1; last; } close $f; if ($found){ print "Found $answer!\n"; }else{ print "Sorry - $answer was not found\n"; } }

            ...Disinformation is not as good as datinformation.               Don't document the program; program the document.

Re: How to read the file from stdin using perl?
by tobyink (Canon) on May 09, 2017 at 10:42 UTC

    Do you mean you want to be given a filename as STDIN, then open that file and read your data from it?

    Or do you mean you want to read your data directly from STDIN?

      i want to read my data directly from STDIN?
        Reading STDIN and if exists inside my file then it should print the whole file?
Re: How to read the file from stdin using perl?
by BillKSmith (Monsignor) on May 09, 2017 at 19:14 UTC
    I believe that you want to print either the contents of the file or an error message depending on the word that the user enters. For a small file, you can slurp it into a string and search it with a regular expression.
    C:\Users\Bill\forums\monks>type gopikavi.pl use strict; use warnings; open my $F, '<', 'file.txt' or die $!; my $text = do{ local $/ = undef; <$F> }; print 'Enter the name: '; chomp( my $name = <> ); if ($text !~/\b$name\b/){ die "Word '$name' is not in file 'file.txt'\n"; } print $text; C:\Users\Bill\forums\monks>perl gopikavi.pl Enter the name: data1 data1 data2 data3 data4 C:\Users\Bill\forums\monks>perl gopikavi.pl Enter the name: data5 Word 'data5' is not in file 'file.txt'
    Bill
Re: How to read the file from stdin using perl?
by Anonymous Monk on May 09, 2017 at 07:53 UTC