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'
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.