Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

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

by frank5us (Initiate)
on Dec 26, 2021 at 06:03 UTC ( [id://11139904]=perlquestion: print w/replies, xml ) Need Help??

frank5us has asked for the wisdom of the Perl Monks concerning the following question:

First, some background info. I had working perl modules for Windows 10 running DWIM perl for many years. Got a new PC last week, windows 11. Dwimperl was having problems installing with cpan for spreadsheet modules, and so I decided to remove DWIMPerl and install the latest Strawberry perl instead. cpan installed my needed spreadsheet modules correctly, so some of my code was now working. But in this module, I get this error: Wide character at C:\perl-lib-ctp/Fmsc.pm line 27, <INFILE> line 1.
sub readfile_to_arr { # take in unicode utf8 text, return as array of +lines my $infilename = shift; my @lines = (); use Encode; open INFILE, "<:utf8" , $infilename || die ("can't open $infilena +me") ; (line 27) while (line 28) (my $line = decode_utf8(<INFILE>)) { push(@lines, $line); }
What's the error message complaining about? A wide character in my source code (which used to work fine on Windows 10 with DWIMperl), or is it complaining about contents of the actual <INFILE> being read in?
  • Comment on Wide character at C:\perl-lib-ctp/Fmsc.pm line 27, <INFILE> line 1.
  • Download Code

Replies are listed 'Best First'.
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

    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>;

      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.

      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
      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: perlquestion [id://11139904]
Approved by kcott
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-25 13:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found