views:

65

answers:

2

I'm applying UTF-8 encoding to STDIN and STDOUT. However how do I make sure that I apply UTF-8 encoding to the file that I pass to my code below (<> will read from a file instead of STDIN if a text file is passed on the command line) in as few lines as possible.

use open qw(:std :utf8)

while (<>) {
    print;
}
+1  A: 

According to the open pragma's documentation, you've already got the behavior you want:

The open pragma serves as one of the interfaces to declare default "layers" (also known as "disciplines") for all I/O. Any two-argument open, readpipe (aka qx//) and similar operators found within the lexical scope of this pragma will use the declared defaults. Even three-argument opens may be affected by this pragma when they don't specify IO layers in MODE.

The perlop documentation tells us that while (<>) { ... } is equivalent to

   unshift(@ARGV, '-') unless @ARGV;
   while ($ARGV = shift) {
     open(ARGV, $ARGV);
     while (<ARGV>) {
       ... # code for each line
     }
   }
Greg Bacon
So then the filehandle is treated as if it's coming from STDIN. I'm reading the perlop documentation, but it doesn't explicitly state that, does it?
syker
@syker No, it uses the `ARGV` filehandle as quoted above. The quoted excerpt from the `open` pragma's documentation specifies that it applies to *all* I/O.
Greg Bacon
A: 

You use the search function on this site: http://stackoverflow.com/questions/519309/how-do-i-read-utf-8-with-diamond-operator/519359#519359

Your question has already been asked and answered.

Armando