Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Wide character at C:\perl-lib-ctp/Fmsc.pm line 27, <INFILE> line 1.

by Corion (Patriarch)
on Dec 26, 2021 at 08:27 UTC ( [id://11139906]=note: print w/replies, xml ) Need Help??


in reply to Wide character at C:\perl-lib-ctp/Fmsc.pm line 27, <INFILE> line 1.

These two lines in combination make little sense:

open INFILE, "<:utf8" , $infilename || die ("can't open $infilena +me") ; while (my $line = decode_utf8(<INFILE>)) { }

The first line already tells Perl to decode the lines in INFILE as UTF-8 bytes.

The second line tries to decode again.

I would lose the second line and rewrite it as:

open INFILE, "<:utf8" , $infilename || die ("can't open $infilena +me") ; @lines = <INFILE>;

Replies are listed 'Best First'.
Re^2: Wide character at C:\perl-lib-ctp/Fmsc.pm line 27, <INFILE> line 1.
by Fletch (Bishop) on Dec 26, 2021 at 14:30 UTC

    Precedence problem there too; you need or instead of ||, or you need to parenthesize open's arguments. As is that's checking if $infilename is true (calling die if not), and then calling open and ignoring the return from it.

    open( FH, MODE, FILE ) || die ERROR; open FH, MODE, FILE or die ERROR;

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re^2: Wide character at C:\perl-lib-ctp/Fmsc.pm line 27, <INFILE> line 1.
by Anonymous Monk on Dec 26, 2021 at 16:53 UTC

    Slight correction: the "<:utf8" asserts that the input file is UTF-8, and it is decoded as such. The preferred mode for this case these days is "<:encoding(utf-8)", which actually checks the encoding, failing if an invalid encoding is encountered. Yes, "<:utf8" appeared in older Perl documentation, which shows that better people than me failed to appreciate the distinction in the early going. It is mostly gone now.

      Thanks, I've incorporated that. When I write the utf-8 as a file, do I also need to wrap it with that check?
      ###################################################################### +################## sub write_file_utf8 { my $outfilename= shift; # appears in browser tabs my $outstr = shift; # file as a string ################################################################## +############## open OUTFILE, ">:utf8" , $outfilename or die "cannot open >:utf8 $outfilename: $!"; #binmode STDOUT, ":utf8"; print OUTFILE $outstr, "\n"; close(OUTFILE); } #sub
Re^2: Wide character at C:\perl-lib-ctp/Fmsc.pm line 27, <INFILE> line 1.
by frank5us (Initiate) on Dec 26, 2021 at 22:55 UTC
    thanks! that made the 'wide char' error go away and it works now. I guess DWIMPerl didn't care about that error.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-04-19 08:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found