Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: using binmode() to override default encoding specified in "use open"

by ikegami (Patriarch)
on Jul 23, 2020 at 04:27 UTC ( [id://11119694]=note: print w/replies, xml ) Need Help??


in reply to using binmode() to override default encoding specified in "use open"

Just use

open(FILE, '>:encoding(UTF-8)', 'fpo');

Notes:

  • Don't needlessly use global vars. Use open(my $FILE, ...) instead of open(FILE, ...).
  • Please avoid the two-arg form of open.
  • open is very prone to failing. Should add some error checking.
  • "utf8" is an extension to UTF-8 used by Perl internally. You want "UTF-8", not "utf8". (Case doesn't matter.)
my $qfn = 'fpo'; open(my $FILE, '>:encoding(UTF-8)', $qfn) or die("Can't create \"$qfn\": $!\n");

Replies are listed 'Best First'.
Re^2: using binmode() to override default encoding specified in "use open"
by raygun (Scribe) on Jul 23, 2020 at 19:04 UTC

    Thank you for the response. The fourth paragraph of my initial post explains why I'm using binmode rather than this solution.

    Your additional notes are good advice, but I omitted error checking, etc., from my example because I simplified my code to include only the relevant bits. I apologize for not stating this explicitly; I wrongly presumed it was clear from constructions like if 1 that are pointless in production code. (In practice I use autodie to avoid having to individually check every open with identical logic or to write my own open wrapper.)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-24 23:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found