Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Issue of formatting columns of data

by choroba (Cardinal)
on Sep 25, 2024 at 15:49 UTC ( [id://11161938]=note: print w/replies, xml ) Need Help??


in reply to Issue of formatting columns of data

You need to
  1. Save the source file as UTF-8.
  2. Tell Perl that it is UTF-8.
  3. Tell Perl to encode the output as UTF-8.
#! /usr/bin/perl use warnings; use strict; use utf8; use open IO => ':encoding(UTF-8)', ':std'; print sprintf ("%-6s", "°C"). "|\n"; print sprintf ("%-6s", "Deg C")."|\n";
Output (in a UTF-8 terminal):
°C    |
Deg C |

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: Issue of formatting columns of data
by Timka (Acolyte) on Sep 26, 2024 at 03:44 UTC
    Any of these would work:

    • binmode STDOUT, "encoding(UTF-8)"
    • binmode STDOUT, ":utf8"
    • use open qw(:std :utf8);
    • perl -C your_script
      • *please* do NOT promote binmode ..., ":utf8";, as that is unsafe
      • The :encoding(utf-8)" is missing a :
      • perl -C is for output only (I'd use -COE for clarity), not for the source code. UTF-8 in source code requires perl -COE -Mutf8 -wE'say "°C"'

      Enjoy, Have FUN! H.Merijn

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2025-06-20 14:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.