refactor: extract file-reading logic into a helper function
This commit is contained in:
26
m2h.pl
26
m2h.pl
@@ -23,6 +23,19 @@ sub show_version {
|
|||||||
exit 0;
|
exit 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub read_input {
|
||||||
|
my ($file) = @_;
|
||||||
|
local $/;
|
||||||
|
if ($file) {
|
||||||
|
open my $fh, '<', $file
|
||||||
|
or die "Error: Cannot open file: $file\n";
|
||||||
|
my $content = <$fh>;
|
||||||
|
close $fh;
|
||||||
|
return $content;
|
||||||
|
}
|
||||||
|
return <STDIN>;
|
||||||
|
}
|
||||||
|
|
||||||
my $output_file;
|
my $output_file;
|
||||||
my $input_file;
|
my $input_file;
|
||||||
|
|
||||||
@@ -46,18 +59,7 @@ for ( my $i = 0 ; $i < @ARGV ; $i++ ) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
my $input;
|
my $input = read_input($input_file);
|
||||||
if ($input_file) {
|
|
||||||
open my $fh, '<', $input_file
|
|
||||||
or die "Error: Cannot open file: $input_file\n";
|
|
||||||
local $/;
|
|
||||||
$input = <$fh>;
|
|
||||||
close $fh;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
local $/;
|
|
||||||
$input = <STDIN>;
|
|
||||||
}
|
|
||||||
|
|
||||||
my $output;
|
my $output;
|
||||||
if ($output_file) {
|
if ($output_file) {
|
||||||
|
|||||||
Reference in New Issue
Block a user