There are the ways halley mentions (Re^2: Empowered by Perl) that are not at all involved. There are many other ways as well. I'm offering some of those others, but it's still not an exhaustive list of ways to do this.

If you're not tied to your syntax, try option 0. If you are, try option 1 or option 2. Note the localization of @ARGV, which is important if you need to specify the content file in the program and still need your command-line arguments. Option 1 always returns a list, so you get a count of lines in scalar context. Option 2 returns whatever the diamond operator read depending on the sub's context. It can be used to read all lines in list context or one line at a time, iteratively, in scalar context.

If you don't need to specify the file you need the contents from in the program and don't care about mangling @ARGV for other uses you can use option 3, which is what automatic @ARGV handling by <> is for. Note that will do the same for as many files as specified in @ARGV and will implicitly shift them off the array.

Option 4 is reminiscent of both Option 0 and options 1/2. It lets you specify a single filename on the command line so you don't have your global @ARGV messed with, but it doesn't use a sub.

Update: Following the same thinking as 0 through 4, option 5 is left for an exercise: Take a command-line argument and pass it to the subroutine from Option 1 or Option 2.

Option 0:

#!usr/bin/perl use warnings; use strict; my @text; { local @ARGV = ( './contents' ); while ( <> ) { push @text, grep /important/, $_; } } print @text, "\n"; print @ARGV, "\n";

Option 1:

#!usr/bin/perl use warnings; use strict; sub contents { local @ARGV = @_; return my @t = <>; } my $text; $text = contents 'contents2'; print $text . "\n"; my @text = grep /important/, contents 'contents2'; print @text, "\n"; print @ARGV, "\n";

Option 2:

#!usr/bin/perl use warnings; use strict; sub contents { local @ARGV = @_; <>; } my $text; $text = contents 'contents3'; print $text . "\n"; my @text = grep /important/, contents 'contents3'; print @text, "\n"; print @ARGV, "\n";

Option 3:

#!usr/bin/perl use warnings; use strict; my @text; while ( <> ) { push @text, grep /important/, $_; } print @text, "\n"; print @ARGV, "\n";

Option 4:

#!usr/bin/perl use warnings; use strict; unless ( defined $ARGV[0] ) { die "Usage: contents4 <filename>\n"; } my @text; { local @ARGV = ( $ARGV[0] ); push @text, grep /important/, <>; } print @text, "\n"; print @ARGV, "\n";

Another approach is to just return what you actually want if you're not looking for all the contents anyway:

#!usr/bin/perl use warnings; use strict; sub grepfile { local @ARGV = ( $_[1] ); return grep /$_[0]/, <>; } my @text = grepfile 'important', './grepfile'; print @text, "\n"; print @ARGV, "\n";

Of course, this is not the prettiest Perl code out there. People might complain that you're messing with your tools in ways other than intended. Others would counter, though, that that's what happens with powerful, successful software.


In reply to Re^2: Empowered by Perl by mr_mischief
in thread Empowered by Perl by liverpole

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.