my $inFH; if ( defined($infile) ) { open my $inFH, '<', $infile or die "opening input file $infile, $!"; } my $instr; { local $/ = undef; $instr = <$inFH> } if ( defined($instr) ) { say "input is $instr"; }
$inFH is being abused! You declare it, then you declare it again in an inner scope. Whatever value it gets from that inner scope is forgotten as soon as it comes out (of that scope). Then you slurp it ... but it's already closed (I mean $inFH) because a filehandle exiting the scope is closed automatically, see When do filehandles close?.
So perhaps something like this?:
my $instr; if ( defined($infile) ) { my $inFH; open $inFH, '<', $infile or die "opening input file $infile, $!"; { local $/ = undef; $instr = <$inFH> } close $inFH; # just polite to + close it } if ( defined($instr) ) { say "input is $instr"; } else { die "sorry, you did not specify an input either via a file or + via [other ways of specifying an instr] " }
In reply to Re^3: chunking up texts correctly for online translation
by bliako
in thread chunking up texts correctly for online translation
by Aldebaran
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |