From your initial shell process, you launch a perl process which launches another shell process which launches another perl process. No, this is not the best way to do things. Except for the system() function, you initially seemed to be on the right track. Here's how I would have tackled this:
#!/usr/bin/env perl
use strict;
use warnings;
my $infile = q{pm_read_part_file.in};
my $outfile = q{pm_read_part_file.out};
my $start = 3;
my $end = 5;
open my $in_fh, q{<}, $infile or die qq{Can't open $infile for reading
+: $!};
open my $out_fh, q{>}, $outfile or die qq{Can't open $outfile for writ
+ing: $!};
while (<$in_fh>) {
next if $. < $start;
last if $. > $end;
print $out_fh $_;
}
close $in_fh;
close $out_fh;
Here's a sample run with file contents:
en@ganymede: ~/tmp
$ pm_read_part_file.pl
ken@ganymede: ~/tmp
$ cat pm_read_part_file.in
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
ken@ganymede: ~/tmp
$ cat pm_read_part_file.out
Line 3
Line 4
Line 5
ken@ganymede: ~/tmp
$
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|