Perl talks

Installing Perl for Win32

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

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 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 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:

How treat CR/LF line endings as LF for STDIN?

binmode STDIN, ':crlf' or die;
 
while (<>) {
    ...
}

See also 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 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

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 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 here1) and here, for 64-bit Perl on 64-bit Windows currently the only supported compiler is the Visual C++ compiler included in the Windows Server 2003 SP1 Platform SDK2). Alternatively (as mentioned here) you can try to use MinGW compiler but you need to download dmake (in particular, dmake-4.1pl1-win32.zip) and unpack it to your PATH.

See also ppm.bat install failed: Can't find any package that provides MinGW.

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'.

1) See chapter “Installing a make utility and C compiler on 64-bit Windows”
2) One need to download PSDK-ia64.exe or PSDK-amd64.exe; see also instructions in README.win32
programming/perl/start.txt · Last modified: 2011/02/05 17:30 by dmitry
 
 
Recent changes RSS feed Driven by DokuWiki