Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

IO::File and use open qw(:std :utf8)

by coolmichael (Deacon)
on Jan 30, 2013 at 02:16 UTC ( #1015948=perlquestion: print w/replies, xml ) Need Help??
coolmichael has asked for the wisdom of the Perl Monks concerning the following question:

IO::File->open() doesn't seem to respect use open() in the following program, which is odd to me and seems to be against the documentation. Or maybe I'm doing it wrong. Rewriting my code to not use IO::File shouldn't be difficult.

Perlmonks is displaying the unicode character U+0213 as &#513 below, but it really is the correct unicode character in the source file.

I expect the output to be

$VAR1 = \"Hello \x{213} (r-caret)"; Hello ȓ (r-caret) Hello ȓ (r-caret) Hello ȓ (r-caret)

But I'm getting this error: "Oops: Malformed UTF-8 character (unexpected end of string) in print at ./ line 33."

That doesn't seem right to me at all.

#!/usr/local/bin/perl use utf8; use v5.16; use strict; use warnings; use warnings qw(FATAL utf8); use diagnostics; use open qw(:std :utf8); use charnames qw(:full :short); use File::Basename; my $application = basename $0; use Data::Dumper; $Data::Dumper::Indent = 1; use Try::Tiny; my $str = "Hello &#531; (r-caret)"; say Dumper(\$str); open(my $fh, '<', \$str); print while ($_ = $fh->getc()); close($fh); print "\n"; try { use IO::File; my $fh = IO::File->new(); $fh->open(\$str, '<'); print while ($_ = $fh->getc()); $fh->close(); print "\n"; } catch { say "\nOops: $_"; }; try { use IO::File; my $fh = IO::File->new(); $fh->open(\$str, '<:encoding(UTF-8)'); print while ($_ = $fh->getc()); $fh->close(); print "\n"; } catch { say "\nOops: $_"; };

Replies are listed 'Best First'.
Re: IO::File and use open qw(:std :utf8)
by mbethke (Hermit) on Jan 30, 2013 at 03:17 UTC

    The open pragma is lexically scoped so its effect doesn't extend to the open() performed internally by IO::File. Unfortunately I don't know if there's a way to make this effect global, short of "insert these opcodes on top of every loaded module"-style hacks.

    Edit: hum, should've read Stackoverflow first ...

Re: IO::File and use open qw(:std :utf8)
by Anonymous Monk on Jan 30, 2013 at 08:42 UTC
Re: IO::File and use open qw(:std :utf8)
by Kenosis (Priest) on Jan 30, 2013 at 03:10 UTC
Re: IO::File and use open qw(:std :utf8)
by choroba (Chancellor) on Jan 30, 2013 at 12:37 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1015948]
Approved by mbethke
[choroba]: Yeah, my wife sent me some pictures of the children yesterday, and there was snow all around. 10 days in Saigon and I totally forgot what weather they have back there.
[stevieb]: We've had so much snow here this year that the whole area is pretty much under constant avalanche warnings (and avalanches pretty much daily all over the place)

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (11)
As of 2017-02-23 17:21 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (350 votes). Check out past polls.