open pragma appears to have no effect

Discussion in 'Perl Misc' started by Rasmus Villemoes, Jan 24, 2010.

  1. 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".

    --
    Rasmus Villemoes
    <http://rasmusvillemoes.dk/>
    Rasmus Villemoes, Jan 24, 2010
    #1
    1. Advertising

  2. Rasmus Villemoes <> writes:

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

    --
    Rasmus Villemoes
    <http://rasmusvillemoes.dk/>
    Rasmus Villemoes, Jan 24, 2010
    #2
    1. Advertising

  3. Rasmus Villemoes

    sreservoir Guest

    On 1/24/2010 9:02 AM, Rasmus Villemoes wrote:
    > Rasmus Villemoes<> writes:
    >
    >> 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.


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

    "Six by nine. Forty two."
    "That's it. That's all there is."
    "I always thought something was fundamentally wrong with the universe"
    sreservoir, Jan 24, 2010
    #3
  4. sreservoir <> writes:

    > On 1/24/2010 9:02 AM, Rasmus Villemoes wrote:
    >> Rasmus Villemoes<> writes:
    >>
    >>> 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.

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

    --
    Rasmus Villemoes
    <http://rasmusvillemoes.dk/>
    Rasmus Villemoes, Jan 24, 2010
    #4
  5. Rasmus Villemoes

    sreservoir Guest

    On 1/24/2010 3:42 PM, Rasmus Villemoes wrote:
    > sreservoir<> writes:
    >
    >> On 1/24/2010 9:02 AM, Rasmus Villemoes wrote:
    >>> Rasmus Villemoes<> writes:
    >>>
    >>>> 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.

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


    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.)
    >
    >> 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


    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.

    --

    "Six by nine. Forty two."
    "That's it. That's all there is."
    "I always thought something was fundamentally wrong with the universe"
    sreservoir, Jan 24, 2010
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Server Control
    Replies:
    0
    Views:
    743
    Server Control
    Mar 21, 2006
  2. Jimmy

    Why #pragma pack not take effect?

    Jimmy, Jul 3, 2007, in forum: C Programming
    Replies:
    5
    Views:
    770
    Kenny McCormack
    Jul 4, 2007
  3. Martin Bachwerk
    Replies:
    3
    Views:
    247
    Diez B. Roggisch
    Oct 15, 2008
  4. Martin Bachwerk
    Replies:
    1
    Views:
    393
    Lawrence D'Oliveiro
    Oct 17, 2008
  5. Simon Strandgaard
    Replies:
    3
    Views:
    99
    Simon Strandgaard
    Apr 1, 2004
Loading...

Share This Page