open pragma appears to have no effect


R

Rasmus Villemoes

Hi group

I am having trouble with the open pragma. If I understand "perldoc
open", I should be able to set the default output mode to utf8 by
saying "open OUT => ':utf8';". However, it seems that I still need to
explicitly append :utf8 when openening a file for output. It is far
more likely that there is something I have misunderstood...

=== utf8test.pl ===
#!/usr/bin/perl

use strict;
use warnings;

use open OUT => ':utf8';

use encoding 'utf8';

use Devel::peek;

use HTML::Entities;

my $html = 'Nørre Allé';

#Dump($html);
decode_entities($html);
Dump($html);
open T, '>', 't1.txt';
print T "$html\n";
close T;

open T, '>:utf8', 't2.txt';
print T "$html\n";
close T;
=== end ===

This is the output I get:

$ ./utf8test.pl && file t*.txt && ls -l t*.txt
SV = PV(0x1801660) at 0x180c720
REFCNT = 1
FLAGS = (PADBUSY,PADMY,POK,pPOK,UTF8)
PV = 0x301f90 "N\303\270rre All\303\251"\0 [UTF8 "N\x{f8}rre All\x{e9}"]
CUR = 12
LEN = 25
t1.txt: ISO-8859 text
t2.txt: UTF-8 Unicode text
-rw------- 1 burner burner 11 Jan 24 12:27 t1.txt
-rw------- 1 burner burner 13 Jan 24 12:27 t2.txt


It doesn't matter if I remove the "use open". If I remove "use
encoding", the only difference is that $html doesn't have a UTF8 flag
(but t2.txt still is valid utf8). Interestingly, if I Dump $html
before the decode_entities, the second Dump produces a few more lines
("MAGIC" stuff).

I have $LANG = da_DK.iso8859-1 and "This is perl, v5.8.6 built for
darwin-thread-multi-2level".
 
Ad

Advertisements

R

Rasmus Villemoes

Rasmus Villemoes said:
open T, '>', 't1.txt';
print T "$html\n";
close T;

A little further experimentation shows that if I change to the
two-argument form "open T, '>t1.txt';" I do get UTF-8.
 
S

sreservoir

A little further experimentation shows that if I change to the
two-argument form "open T, '>t1.txt';" I do get UTF-8.

When open() is given an explicit list of layers (with the
three-arg syntax), they override the list declared using this pragma.

from the documentation. try >:utf8, because presumably, perl treats any
three-arg as having specified the layers.
 
R

Rasmus Villemoes

sreservoir said:
When open() is given an explicit list of layers (with the
three-arg syntax), they override the list declared using this pragma.

from the documentation.

I can't find that in my "perldoc -f open", the closest is this remark

(Note that if layers are specified in the three-arg form then
default layers set by the "open" pragma are ignored.)
try >:utf8, because presumably, perl treats any three-arg as having
specified the layers.

But is '>' "an explicit list of layers"? I would assume that when no
layer is specified, some default is used. Why is that default not
taken from the open pragma? In other words, why does

open T, '>', 'file.txt'

and

open T, '>file.txt'

behave differently? perldoc open says "When open() is given an
explicit list of layers they are appended to the list declared using
this pragma.", and I would say that '>' specifies an empty list of
layers.
 
Ad

Advertisements

S

sreservoir

I can't find that in my "perldoc -f open", the closest is this remark

perldoc open, not the function.
(Note that if layers are specified in the three-arg form then
default layers set by the "open" pragma are ignored.)


But is '>' "an explicit list of layers"? I would assume that when no
layer is specified, some default is used. Why is that default not
taken from the open pragma? In other words, why does

probably shouldn't.
open T, '>', 'file.txt'

and

open T, '>file.txt'

why do open T, "> aaa " and open T, '>', " aaa " behave differently?
behave differently? perldoc open says "When open() is given an
explicit list of layers they are appended to the list declared using
this pragma.", and I would say that '>' specifies an empty list of
layers.

empty list is a list nonetheless.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top