Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Special character in file name -- die error messed up

by leszekdubiel (Scribe)
on May 28, 2020 at 08:53 UTC ( [id://11117393]=perlquestion: print w/replies, xml ) Need Help??

leszekdubiel has asked for the wisdom of the Perl Monks concerning the following question:

Is there any way to make "die" handle polish file names better? It's all about chars in file name. See:

# cat ąćęłńó.pl 
#!/usr/bin/perl -CSDA

use utf8; 

die "language specific chars:  ąćęłńó"; 


# ./ąćęłńó.pl 
language specific chars:  ąćęłńó at ./Ä
ÄÄÅÅ
    ó.pl line 5.
  • Comment on Special character in file name -- die error messed up

Replies are listed 'Best First'.
Re: Special character in file name -- die error messed up
by haj (Vicar) on May 28, 2020 at 11:54 UTC

    This is a bit tricky and a known shortcoming of Perl which isn't all that easy to fix.

    To start with, Perl does not know which encoding the file system applies to represent polish characters, and you have no way of specifying it. So, the name of your source file is known to Perl as bytes, encoded in whatever encoding the file system chooses. Nowadays, unless you are on Windows, UTF-8 is a reasonable guess, but Perl does not guess.

    Your command line parameter -CS then enforces UTF-8 encoding of STDERR, which is fine for the characters in your error message but not for file names. So, you get the file name doubly encoded.

    One way to get out of this is to apply encoding yourself, and drop the command line parameters. This sort of works, but makes the assumption that the file name is encoded in UTF-8:

    #!/usr/bin/perl
    # saved as ąćęłńó.pl

    use utf8;

    use Encode qw(encode);

    die encode('UTF-8',"language specific chars: ąćęłńó");

    Edited because I forgot (again) that the code tags break non-ASCII characters

Re: Special character in file name -- die error messed up
by soonix (Canon) on May 28, 2020 at 12:30 UTC
Re: Special character in file name -- die error messed up
by leszekdubiel (Scribe) on May 28, 2020 at 20:41 UTC
    Tanks for help. I will live with that inconvenience.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11117393]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-26 04:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found