我对编码还很陌生,我需要一个失败语句来打印出来,就好像它是一个或死一样。
我的部分代码示例:
print "Please enter the name of the file to search:";
chomp (my $filename=<STDIN>) or die "No such file exists. Exiting program. Please try again."\n;
print "Enter a word to search for:";
chomp (my $word=<STDIN>);
我需要它来为这两个 print/chomp 语句做这件事。反正有什么可以补充的吗?
整个程序:
#!/usr/bin/perl -w
use strict;
print "Welcome to the word frequency calculator.\n";
print "This program prompts the user for a file to open, \n";
print "then it prompts for a word to search for in that file,\n";
print "finally the frequency of the word is displayed.\n";
print " \n";
print "Please enter the name of the file to search:";
while (<>){
print;
}
print "Enter a word to search for:";
chomp( my $input = <STDIN> );
my $filename = <STDIN>;
my$ctr=0;
foreach( $filename ) {
if ( /\b$input\b/ ) {
$ctr++;
}
}
print "Freq: $ctr\n";
exit;