I ran through some various scenarios for you on a Linux machine...see below. To get the results of a "backtick, `" command, normally you should use an "@" variable when multiple lines could happen. In Perl when you assign an array to a scalar like: $number = @array, $number is the number of things in @array. So: "if (@array){}" gives you a way to test whether or not something is in the @array or not.

Also, as a general rule: if a subroutine opens a file, it should be responsible for closing it. A subroutine shouldn't leave a file open when it returns. Explicit closes are not required in the main program as all open file handles are closed when the main program exits. But if you pass a file name to something like my find_matching_lines() subroutine, you should close that file when you are finished with it, otherwise open the file the main program and pass a filehandle to the subroutine.

#!/usr/bin/perl -w use strict; print "*the backtick cat results:*\n"; print `cat /etc/filesystems`; #prints: # ext3 # ext2 # nodev proc # nodev devpts # iso9660 # vfat # hfs # hfsplus print "\n"; #spacer line my @catlines = `cat /etc/filesystems`; print "*the catlines array:*\n"; print @catlines; #prints same as backtick results above print "\n"; #spacer line my $file = '/etc/filesystems'; my $pattern = 'nodev'; my @lines= `grep $pattern $file`; print "*the grep backtick results:*\n"; print @lines; print "\n"; #spacer line #prints: # nodev proc # nodev devpts my @getlines = find_matching_lines($file,'ext'); print "*the getlines array*\n"; print scalar(@getlines), " lines found\n"; my $numlines = @getlines; print "number of lines = $numlines\n"; #same thing print "".@lines," lines found\n"; #the concatentation op "." forces sc +alar print @getlines; sub find_matching_lines { my ($file_name, $pattern)=@_; my @matches =(); open (my $in, '<', $file_name) or die "unable to open $file_name"; while (<$in>) { push (@matches,$_) if m/$pattern/; } close ($in); return @matches; } __END__ *the backtick cat results:* ext3 ext2 nodev proc nodev devpts iso9660 vfat hfs hfsplus *the catlines array:* ext3 ext2 nodev proc nodev devpts iso9660 vfat hfs hfsplus *the grep backtick results:* nodev proc nodev devpts *the getlines array* 2 lines found number of lines = 2 2 lines found ext3 ext2

In reply to Re: subroutine to search a pattern in a file by Marshall
in thread subroutine to search a pattern in a file by dilip_val

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.