Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Unicode, regex's, encodings, and all that (Perl 5.6 and 5.8)

by pg (Canon)
on Dec 24, 2002 at 07:57 UTC ( [id://222067]=note: print w/replies, xml ) Need Help??


in reply to Unicode, regex's, encodings, and all that (Perl 5.6 and 5.8)

Yes, 'use encoding'/'no encoding' are per script, but not per block lexical. However your interpretation/understanding is wrong. The following examples could help you to understand:
  1. In a package, if you say:
    ... (block 1)
    use encoding "greek";
    ... (block 2)
    no encoding;
    ... (block 3)
    
    The actual effect is encoding in block 2, but no encoding in block 1, and 3. It is not as you expected that, becasue that no encoding comes the last, the whole module is no encoding.
  2. In a package, if you say:
    ... (block 1)
    use encoding "greek";
    ... (block 2)
    use encoding "greek";
    ... (block 3)
    no encoding;
    ... (block 4)
    
    The actual effect is encoding in block 2 and 3, but no encoding in block 1 and 4. Again, according to your interpretation, the whole module is not encoding becasue that 'no encoding' is the last in the physical sequence. And again your interpretation is wrong.
  3. NOW WHAT DOES THAT SENTENCE IN "encoding" documentation MEAN? Let's get to the point:

    In packageA, you say:
    ...(block 1)
    use encoding "greek";
    ...(block 2)
    
    In a script, you say:
    use packageA;
    ...(block3)
    
    In this example, you should expect, block 1 not encoding, block 2 encoding. Block 3? Now this is what the document means: block 3 is encoding, because that use encoding in packageA is not block lexical, but per script, and your script now 'contains' packageA, that use encoding you put in packageA does affect the rest of the script, not just to the end of packageA.
  4. Another example: In packageA, you say:
    ...(block 1)
    use encoding "greek";
    ...(block 2)
    
    In a script, you say:
    use packageA;
    ...(block3)
    no encoding;
    ...(block 4)
    
    It would be encoding in block 2 and 3, no encoding in 1 and 4. The reason block 3 is encoding is that, the 'use encoding' you put in packageA is per script, so it even affects things outside the package, until meets a 'no encoding' later in the script.
  5. Last example, and this is what you are supposed to do in the best practice.

    In packageA, you say:
    ...(block 1)
    use encoding "greek";
    ...(block 2)
    no encoding; # at the end of packageA
    
    In a script, you say:
    use packageA;
    ...(block 3)
    use encoding "greek";
    ...(block 4)
    
    Block 1, 3 not encoing, 2 and 4 encoding, exactly meets what you visually see on the screen, what you see is what you get. So the best practice is to always say "no encoding" at the end of your module, if you said any "use encoding" earlier in your module. This makes sure that your module's encoding does not affect any other module/script unexpectedly.

Replies are listed 'Best First'.
Re: Re: Unicode, regex's, encodings, and all that (Perl 5.6 and 5.8)
by John M. Dlugosz (Monsignor) on Dec 24, 2002 at 19:36 UTC
    Ah,
    The pragma is a per script, not a per block lexical. Only the last use encoding or no encoding matters, and it affects the whole script.
    Really should say something like
    The pragma is not block-scoped like lexicals and strict. Rather, any use/no encoding statement has an immediate effect starting on the next statement in the file. It does not "pop" at the end of the block or current module, but continues on until another use or no encoding statement is encountered.

    FWIW, I don't think I'm "interpreting" the sentence "only the last one matters and affects the whole script" in an odd way; I think it is written incorrectly and says the wrong thing.

    —John

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-03-28 22:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found