====== Perl talks ======
===== Installing Perl for Win32 ======
* [[http://cygwin.com/|CygWin]] comes with Perl.
* [[http://www.activestate.com/activeperl/downloads|ActivePerl]] comes with many popular Perl packages included. Use ''ppm install That::Module'' or read [[https://www.activestate.com/blog/2010/10/how-install-cpan-modules-activeperl|How to install CPAN modules into ActivePerl]] to install the missing ones.
===== Questions answered ======
=== How to iterate over associated array (hash)? ===
By key and value:
while (my ($key, $value) = each %my_hash) { ... }
By sorted keys:
foreach my $key (sort keys %my_hash) { ... }
=== How to suppress ''Setting locale failed'' message ===
If you see this message:
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
uncomment the locale you need in ''/etc/locale.gen'' and run ''locale-gen''.
=== How to strace all executed lines of perl script? ===
# Install Devel::Trace:
apt-get install libdevel-trace-perl
# Run the script:
perl -d:Trace my_script.pl
=== [[stackoverflowa>2037519/267197|How force Perl to treat command-line arguments as UTF-8?]] ===
You need to pass ''-CA'' command-line argument to Perl interpreter:
perl -CA -e 'binmode(STDOUT, ":utf8"); print $ARGV[0]. "\n";' unicode_file_name
=== UTF-8 encoded query parameter is not correctly decoded into string ===
As to [[https://www.lemoda.net/cgi/perl-unicode/index.html|Using Unicode in a Perl CGI script]], query parameter should be decoded into internal Perl representation using ''decode_utf8()''.
use Encode qw(decode_utf8);
$text = $dbh->quote(decode_utf8($text));
$dbh->do("insert into MYTABLE (TEXT) values ($text)");
The same concerns subject and body returned by MIME::Parser, see [[http://perlhackerpainter.blogspot.nl/2010/02/perl-utf-8-email-messages-mimeenity-and.html|Perl, UTF-8 Email Messages, MIME::Enity]]:
use Encode qw(decode_utf8);
use MIME::Parser;
$text = decode_utf8(MIME::Parser->new()->read(\*STDIN)->head()->get('Subject'));
See also:
* [[http://www.perlmonks.org/?node_id=620803|A UTF8 round trip with MySQL]]
* [[stackoverflow>983778|How can I handle unicode with Perl's DBI?]]
=== How treat CR/LF line endings as LF for ''STDIN''? ===
binmode STDIN, ':crlf' or die;
while (<>) {
...
}
See also [[stackoverflowa>7536079/267197|CR vs LF perl parsing]].
=== How to decode Base64 string? ===
Decode Base64-encoded string:
$ echo "w7zDtsOkw58K" | perl -MMIME::Base64 -pe '$_ = MIME::Base64::decode($_) . "\n";'
üöäß
Decode Base64-encoded binary string into number:
$ echo AQUAAAAAAAUVAAAA9TZFSbZE5CNDFwoyvJUBAA== | perl -MMIME::Base64 -MMath::BigInt -pe '$_ = Math::BigInt->new("0x" . join("", map sprintf("%.2x", ord($_)), split //, MIME::Base64::decode($_))) . "\n";'
107369172365208701678008732329624989801437041400940127167586828544
Decode Base64-encoded [[wp>Security_Identifier|SID]]:
$ echo AQUAAAAAAAUVAAAA9TZFSbZE5CNDFwoyvJUBAA== | perl -MMIME::Base64 -pe '$_ = "S-1-5-21-" . join("-", unpack "VVVV", substr(MIME::Base64::decode($_), -16)) . "\n";'
S-1-5-21-1229272821-602162358-839522115-103868
=== How to decode hex-string into characters? ===
# The given string should be interpreted as 0x44,0x41,0x54,0x45,0x20,0x4f,0x46 and then printed as string of corresponding characters:
$ perl -e 'print pack("H*", "44415445204f46") . "\n"'
DATE OF
=== [[http://ubuntuforums.org/showthread.php?t=885344|How to sort ''du'' output?]] ===
$ du -sh * | perl -e '%byte_order = ( G => 0, M => 1, K => 2, k => 2 ); print map { $_->[0] } sort { $byte_order{$a->[1]} <=> $byte_order{$b->[1]} || $b->[2] <=> $a->[2] } map { [ $_, /([MGKk])/, /(\d+)/ ] } <>'
but it is more wise to use ''du -sh | sort -rh''
=== How to extract email body from message? ===
Quick solution, although a bit dirty:
cat mime_mail | perl -MMIME::Parser -e 'print MIME::Parser->new()->read(\*STDIN)->bodyhandle()->as_string();'
More clean solution is [[svn>trunk/programming/perl/common/Mail.pm|here]].
=== How to enable installation of modules using CPAN on x64 platform? ===
With ActivePerl on x64, the command ''perl -MCPAN -e shell'' fails with following message:
ppm.bat install failed: Can't find any package that provides MinGW
As stated [[http://www.activestate.com/blog/2010/10/how-install-cpan-modules-activeperl|here]]((See chapter "Installing a make utility and C compiler on 64-bit Windows")) and [[http://community.activestate.com/faq/windows-compilers-perl-modules|here]], for 64-bit Perl on 64-bit Windows currently the only supported compiler is the Visual C++ compiler included in the [[http://www.microsoft.com/downloads/details.aspx?FamilyId=A55B6B43-E24F-4EA3-A93E-40C0EC4F68E5|Windows Server 2003 SP1 Platform SDK]]((One need to download ''PSDK-ia64.exe'' or ''PSDK-amd64.exe''; see also instructions in ''[[http://search.cpan.org/~rjbs/perl-5.16.0/README.win32|README.win32]]'')). Alternatively (as mentioned [[http://www.perlmonks.org/?node_id=583586#583656|here]]) you can try to use [[http://www.mingw.org/download.shtml|MinGW compiler]] but you need to download ''[[http://search.cpan.org/dist/dmake/|dmake]]'' (in particular, ''[[http://www.cpan.org/authors/id/GSAR/dmake-4.1pl1-win32.zip|dmake-4.1pl1-win32.zip]]'') and unpack it to your ''PATH''.
See also [[stackoverflow>10545976|ppm.bat install failed: Can't find any package that provides MinGW]].
=== [[stackoverflowa>3736710/267197|How to install Perl modules to my ''$HOME''?]] ===
* Download and unpack the ''local::lib'' tarball from CPAN.
* Run ''perl Makefile.PL --bootstrap''
* Run ''make test && make install''
After that one can run e.g. ''%%perl -I~/perl5 -MCPAN -Mlocal::lib -e 'install DBD'%%''.
{{tag>Perl CPAN}}