Check which character encodings are supported by Perl
Answer:
Use the following command to check which character encodings are supported by Perl.
# perl -MEncode -le "print for Encode->encodings(':all')"
Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.
Check which character encodings are supported by Perl
Answer:
Use the following command to check which character encodings are supported by Perl.
# perl -MEncode -le "print for Encode->encodings(':all')"
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";
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;
Counting the number of pair(s) in Perl's hash
Answer:
If you have a Perl's hash variable, e.g. %hash, you can count the total number of hash pair (i.e. key/value) in that variable, like the following.
my $count = scalar keys %hash;
Remove the last character of a string in Perl
Answer:
If you want to remove the last character of a string in Perl, e.g.
abc => ab
You can use the chop() function.
my $string = 'abc';
chop($string);
print $string; # give ab