Your arguments are getting weaker. Just be you can write it that way, it does mean you have to.
You could write it as below. And if that makes you feel that you've done a "proper job", then you're welcome to, but it'll just take you longer, be no more "reliable"; and is no more good or "proper programming" than the one-liner.
#! perl
use strict;
use warnings;
use constant {
FILENAMEROOT => 'split.',
OPEN_MODE_OUTPUT => '>',
OPEN_MODE_INPUT => '<',
SELECTOR_OFFSET => 3,
SELECTOR_LENGTH => 1,
ZERO => 0,
ONE => 1,
TEN => 10,
};
############################
# Main program starts here #
############################
################################
# An array to hold filehandles #
################################
my @fileHandles;
######################################
# iterate from 0 to 9 #
# compose a filename incorporating #
# the number of the split file #
# open the file for output #
# check that it opend successfully #
# push the filehandle onto the array #
# of filehandles index by the number #
######################################
for(
my $index = ZERO;
$index < TEN;
$index = $index + ONE
) {
## A varible to hile the composed filename
my $fileName = FILENAMEROOT . $index;
# another to hold the file handle
my $fileHandle;
# open the file
my $rc = open $fileHandle, OPEN_MODE_OUTPUT, $fileName;
# check it opened successfully, or die
if( $rc == 0 ) {
die "Couldn't open " . $fileName
. " for output; rc = "
. $rc . "; Errortext = " . $!;
}
# else add it to the array of filehandles
else {
push @fileHandles, $fileHandle;
}
}
die "You must supply an input filename" unless scalar @ARGV == 1;
# get the name of the input file
my( $inputFileName ) = @ARGV;
die "The filename you supply must exist" unless -e $inputFileName;
die "The file must contain something" if -z $inputFilename;
# open the input file
$rc = open my $inputFileHandle, INPUT_MODE, $inputFileName;
# Did it open? Die if not.
if( not $rc == 0 ) {
die "couldn't open $inputFileName: $!";
}
# iterate over the input file one line at a time
while(
defined(
my $inputLine = readline( $inputFileHandle )
)
) {
# extract the character by which we select
# the output file to put this line in
my $selectorCharacter = substr(
$inputLine, SELECTOR_OFFSET, SELECTOR_LENGTH
);
# check that it is a digit, otherwise die
if( $selectorCharacter lt '0' or $selectorCharacter gt '9' ) {
die "Selector character is not a digit: " . SelectorCharacter;
}
# selector character is okay
else {
# so get the appropriate filehandle from the array
my $fileHandle = @fileHandles[
ord( $selector ) - ord( S$selectorCharacter )
];
# check that we actually got a filehandle, or die
if( not defined $fileHandle ) {
die "Couldn't obtain fileHandle for Selector character "
. $selectorCharacter;
}
# okay, we've got a filehandle
else {
# so output the file handle to the appropriate file
print $fileHandle $inputLine;
}
}
}
# close the input file
close $inputFileHandle;
# iterate over the output filehandles array
for(
my $fileHandleIndex = 0;
$fileHandleIndex < 10;
$FileHandleIndex = $fileHandleIndex + ONE
) {
# get the output filehandle
my $fileHandle = $fileHandles[ $fileHandleIndex ];
# make sure that it hasn't gone walkies in teh last 3 milliseconds
if( not defined $fileHandle ) {
# and die if it has (leaving all the other filehandles unclose
+d; but what can you do?)
die "Couldn't obtain fileHandle for index " . $fileHandleIndex
+;
}
# okay, we;ve got a filehandle
else {
# so now we can close it. (Shoudl we check that it actually cl
+osed successfully?)
close $fileHandle;
}
}
######################################################################
+########
######################################################################
+########
##
+ ##
## EEEEE N N DDDD PPPP RRRR OOO GGG RRRR A M
+ M ##
## E NN N D D P P R R O O G G R R A A M
+M MM ##
## E N N N D D P P R R O O G R R A A M
+ M M ##
## EEEE N NN D D PPPP RRRR O O G GG RRRR AAAAA M
+ M ##
## E N N D D P R R O O G G R R A A M
+ M ##
## E N N D D P R R O O G G R R A A M
+ M ##
## EEEEE N N DDDD P R R OOO GGG R R A A M
+ M ##
##
+ ##
######################################################################
+########
######################################################################
+########
exit;
More just a wannabe C programmer using Perl... |