The previous replies are more worth pursuing than mine, but just to be picky, I'll point out:

To emulate unix "wc -l", you should accept data from STDIN, and you should accept multiple file name args on the command line, reporting the line count for each file in succession:

#!/usr/bin/perl -w use strict; if ( @ARGV ) { for my $file ( @ARGV ) { $. = 0; open( I, $file ) or do { warn "$0: open failed for file $file: $!\n"; next; } while (<I>) {} printf( "%8d %s\n", $., $file ); } } else { while (<>) {} printf( "%8d\n", $. ); }

As for your version of "split", the only thing I see wrong is that if the line count of an input file happens to be a multiple of the selected line count for output, you'll open an extra file for output, and will close it without having written anything to it. No big deal, except some users might get confused, misled or even tripped up by having an extra file (which happens to be empty) when the process is done. Here's a simplified version that avoids that problem:

#!/usr/bin/perl -w use strict; my $Usage = "Usage: $0 line_count input.file output_name\n"; ( @ARGV == 3 and $ARGV[0] =~ /^\d+$/ and -f $ARGV[1] ) or die $Usage; my ( $line_limit, $infile, $outname ) = @ARGV; my $outid = 0; open( I, $infile ) or die "$0: open for input failed on $infile: $!\n$ +Usage"; while (<IN>) { if ( $. % $line_limit == 1 ) { close OUT if ( $outid ); my $outfile = sprintf( "%s.%03d", $outname, ++$outid ); open( OUT, ">", $outfile ) or die "$0: open for output failed +on $outfile\n"; } print OUT; } close OUT;

In reply to Re: mimicing wc and split by graff
in thread mimicing wc and split by dwhite20899

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.