Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Jun 272010
 

Wide character in print... warning in Perl

Answer:

Assume you have a simple script that print out UTF-8 characters in your script.

E.g.


#!/usr/bin/perl

my $foo = "Hello \x{4E2D} \x{570B}\n";   

print $foo;

When you run it, it will give out warnings...

Wide character in print at test.pl line 5.
Hello 中 國

To fix for the problem, add the line at the top of your program.

use encoding "utf-8";

Jun 262010
 

Run Perl with useful warnings

Answer:

For good programming habits, it is advised to run Perl using the -w flag so it will print out some useful warnings that might be useful for your program.

E.g.

# perl -e 'print $foo';

The above script shows nothing, but with the -w flag,

# perl  -w -e 'print $foo'; 
Name "main::foo" used only once: possible typo at -e line 1.
Use of uninitialized value $foo in print at -e line 1.

So you can see it is very useful for debugging your program.

In fact, the -w flag is the same as the warnings pragma

use warnings;