I'm going to comment your script one line at a time - it really is an excellent opportunity to explain some good bits of Perl (presumably, that's just what the author of your book intended.) My comments are prefixed with a '###'.

#!usr/bin/env perl ### I suspect that you're retyping your code, since the ### above would fail with a "Bad interpreter" error: there ### is no such thing as "usr/bin/env" (note absence of ### initial '/'.) As well, the 'env' trick isn't usually ### necessary unless you're moving your script between ### radically different systems where Perl is located ### in different places; usually, "#!/usr/bin/perl" will do. ### In addition, you should always - yes, *always* - enable ### warnings (i.e., let the computer do your troubleshooting ### for you) - so that shebang line should also have a ' -w' ### added to its end. open (test, "@ARGV"); ### Problem #1: you're trying to use an array (@ARGV) where ### you should be using a string containing a filename. This ### _will_ break if @ARGV contains more than one element. ### Problem #2: Whenever you perform any system-related task ### - i.e., an operation that is external to Perl - you ### should always (yes, *always*) check the result. As a ### minor additional correction, you should elide any ### unnecessary punctuation in your Perl - and you should ### either use sentence-case or all-caps names for file ### handles. As a result, the corrected version of the above ### line should read ### ### open Test, $ARGV[0] or die "$ARGV[0]: $!\n"; while (<test>) { $_ =~ <test>; ### This line is a simple error, and should be omitted. chomp; ### This is unnecessary, since you're going to want ### the newline character when you print the output. ### Omit it. s/(th)/TH/gi; ### Neither grouping nor capturing are used in the ### above regex; therefore, the parentheses do ### nothing and should be omitted. print "$_\n"; ### If you omit the above 'chomp', it will become ### unnecessary to explicitly specify '$_' or '\n'. ### This line then simply becomes 'print;'. } close (test); ### Again, the parens are unnecessary. Omit them.

As a result, the entire script should look like this:

#!/usr/bin/perl -w open Test, $ARGV[0] or die "$ARGV[0]: $!\n"; while (<Test>) { s/th/TH/gi; print; } close Test;

Conversely, you could use Perl's "diamond operator" which will do the same thing without having to explicitly open the file that you've specified on the command line:

#!/usr/bin/perl -w while (<>) { s/th/TH/gi; print; }

In reply to Re: Why are lines being skipped in output? by oko1
in thread Why are lines being skipped in output? by negzero7

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.