2

In my previous question I asked about the problem with encoding and open pragma. Based on @daxim's answer coming to my mind another questions.

use open qw(:std :utf8);
  • It is a good practice using the above open pragma? Asking because I cannot ensure than some CPAN packages does not want do some IO - and the open pragma will interfere with them.

sub-questions:

  • Is possible somewhat detect, what pragma is in effect for the given stream? For example: is possible somewhat detect than STDOUT is opened with the open(:std :utf8) pragma?
  • more broadly, is possible detect what conversions/(or encoding) is applied for any (already opened) file-handle? (if so, it is possible to ensure not getting double encoding.)

Ps: I learned much from the tchrit's famous answer, but seem not enough yet. ;(

Community
  • 1
  • 1
clt60
  • 62,119
  • 17
  • 107
  • 194

1 Answers1

3

You can use PerlIO::get_layers to detect layers assigned to a filehandle:

use open qw(:std :utf8);
open my $in, '<', 'somefile' or die "$!"
my @layers = PerlIO::get_layers($in);      # ("unix", "crlf", "utf8")
bvr
  • 9,687
  • 22
  • 28