Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: chunking up texts correctly for online translation

by Aldebaran (Curate)
on Jul 02, 2019 at 21:39 UTC ( [id://11102317]=note: print w/replies, xml ) Need Help??


in reply to Re: chunking up texts correctly for online translation
in thread chunking up texts correctly for online translation

Struggling to get basic functionality here. I'm removing the option to use STDIN for input, as I have a translate shell already that covers this functionality for me. Parts are working, for example, I die if no value for config is supplied. I can't seem to get to our favorite example text of late:

$ ./1.get_opt.pl --configfile C --outfile /home/bob/Documents/meditati +ons/Algorithm-Markov-Multiorder-Learner-master/output/1.txt --infile +/home/bob/Documents/meditations/Algorithm-Markov-Multiorder-Learner-m +aster/data/2.short.shelley.txt Use of uninitialized value $inFH in <HANDLE> at ./1.get_opt.pl line 34 +. readline() on unopened filehandle at ./1.get_opt.pl line 34. $ cat 1.get_opt.pl #!/usr/bin/perl -w use 5.011; use Getopt::Long; my $outfile = undef; my $configfile = undef; my $infile = undef; if ( !Getopt::Long::GetOptions( "outfile=s", \$outfile, "infile=s", \$infile, "configfile=s", \$configfile, "help", sub { print "Usage : $0 --configfile C [--outfile O] [--infile I] [--h +elp]\n"; exit 0; }, ) ) { die "error, commandline"; } die "configfile is needed (via --configfile)" unless defined $configfi +le; 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"; } $

Replies are listed 'Best First'.
Re^3: chunking up texts correctly for online translation
by bliako (Monsignor) on Jul 03, 2019 at 00:39 UTC
    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] " }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11102317]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-04-19 05:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found