title: With a utf8-encoded Perl script, can it open a filename encoded as GB2312?
body: <p>I'm not talking about reading in the file content in utf-8 or non-utf-8 encoding and stuff. It's about file names. Usually I save my Perl script in the system default encoding, "GB2312" in my case and I won't have any file open problems. But for processing purposes, I'm now having some Perl script files saved in utf-8 encoding. The problem is: these scripts cannot open the files whose names consist of characters encoded in "GB2312" encoding and I don't like the idea of having to rename my files.</p>
  
  <p>Does anyone happen to have any experience in dealing with this kind of situation? Thanks like always for any guidance.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Here's the minimized code to demonstrate my problem:</p>
  
  <pre><code># I'm running ActivePerl 5.10.1 on Windows XP (Simplified Chinese version)
  # The file system is NTFS
  
  #!perl -w
  use autodie;
  
  my $file = "./测试.txt"; #the file name consists of two Chinese characters
  open my $in,'&lt;',"$file";
  
  while (&lt;$in&gt;){
  print;
  }
  </code></pre>
  
  <p>This test script can run well if saved in "ANSI" encoding (I assume ANSI encoding is the same as GB2312, which is used to display Chinese charcters). But it won't work if saved as "UTF-8" and the error message is as follows:</p>
  
  <pre>
  Can't open './娴嬭瘯.txt' for reading: 'No such file or directory'.
  </pre>
  
  <p>In this warning message, "娴嬭瘯" are meaningless junk characters.</p>
  
  <p><strong>Update</strong></p>
  
  <p>I tried first encoding the file name as GB2312 but it does not seem to work :(
  Here's what I tried:</p>
  
  <pre><code>#!perl -w
  use autodie;
  use Encode;
  
  my $file = "./测试.txt";
  encode("gb2312", decode("utf-8", $file));
  open my $in,'&lt;',"$file";
  
  while (&lt;$in&gt;){
  print;
  }
  </code></pre>
  
  <p>My current thinking is: the file name in my OS is 测试.txt but it is encoded as GB2312. In the Perl script the file name looks the same to human eyes, still 测试.txt. But to Perl, they are different because they have different internal representations. But I don't understand why the problem persists when I already converted my file name in Perl to GB2312 as shown in the above code.</p>
  
  <p><strong>Update</strong></p>
  
  <p>I made it, finally made it :)</p>
  
  <p>@brian's suggestion is right. I made a mistake in the above code. I didn't give the encoded file name back to the $file. </p>
  
  <p>Here's the solution:</p>
  
  <pre><code>#!perl -w
  use autodie;
  use Encode;
  
  my $file = "./测试.txt";
  $file = encode("gb2312", decode("utf-8", $file));
  open my $in,'&lt;',"$file";
  
  while (&lt;$in&gt;){
  print;
  }
  </code></pre>