I think you also want to get the $input_file from the command line?.
I personally have a disdain for while(1) loops and pretty much only use them for servers or similar applications.
I would code the loop ending condition into the while loop. If you don't like the comma operator, a logical and should work also.
#!/usr/bin/perl use strict; use warnings; ############## ## 03/14/19 ## Exercise reads from file, then lets user interactively submit matc +hing search criteria my $input_file = shift @ARGV; open my $fh, '<', $input_file or die "unable to open $input_file for s +earching $!"; chomp(my @strings = <$fh>); my $pattern; while ( (print 'Please enter a pattern: '),$pattern=<STDIN>, $pattern +!~ /\A\s*\Z/) { chomp $pattern; my @matches = eval { grep /$pattern/, @strings; }; ## end of eval if ($@) { print "Error: $@"; } else { my $count = @matches; print "There were $count matching strings:\n", join ("\n",@matches); } print "\n"; }
Update: you will notice that I changed a few other things. I don't see anything here that requires Perl >=5.16, so I put in "use strict;". This code should run fine on an ancient Perl. If you use some regex construct that is not available in the Perl being used, your eval will fail at run time which sounds fine to me. Instead of using a map() to add line endings to @matches, I used a join. If Perl has a special function for something, this usually works out faster and makes the code more clear than using a more general, but "heavier weight" mechanism like map. Now that I think about it, there probably isn't any need to take out the line endings in the first place which would render this point mute.

The reason I like my while conditional is that it shows clearly what is going to cause the loop to proceed and what is going to cause it to end (blank line). Note that parens around the print statement is not a typo, they are required here in order to get the characters to the screen "mid way" through the statement. Play with the code using the parens and not using them and you will see what it does. Of course enhancements are possible, like adding an error message if the command line doesn't contain a file name. die "no input file specified!\n" unless defined ($input_file); or whatever, maybe a longer usage message? Just icing on the cake.

Another update on style: In my opinion some of the most important "code" that you can write is "whitespace". Start the high level code at the left margin (you had it indented). Add a blank line when roughly speaking the subject/purpose changes. I put a blank line after getting the input file "ready", after getting the pattern "ready", after using the pattern. Minor quibbles can arise from this - my main point is that whitespace can increase understandability and consumes NO Mips! Consider adding blank lines at places you consider significant "thought transitions".


In reply to Re: Working on pattern matching by Marshall
in thread Working on pattern matching by catfish1116

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.