invalid byte sequence for encoding “UTF8″: 0×8b – storing binary data

This error message took me lots of effort and time to solve it. The problem is that before storing binary data strings in the postgresql database the binary strings need to be escaped before storing them. There is on the google available subroutine for per which is doing this job. It is called escape_bytea. Anyway [...]

Create and install linux debian package for missing perl module with dh-make-perl

Sometimes you encounter situation when you need a perl module to support your program but perl module is not available from standard linux repositories. Installing perl module from source code is not always good idea so what would be the best is to create package for a particular module using dh-make-perl command. In my case [...]

Perl script code example to create temporary file-tempfile()

Here is a perl script example how to create temporary file on the file system:
TEMPLATE DIR AND SUFIX are option !

#!/usr/bin/perl
my $tempfile = new File::Temp ( TEMPLATE => ‘tempfile_XXX’, DIR => ‘/tmp/’, SUFIX => ” );

this will create a temporary file in /tmp/ directory with name tempfile_XXX where X is random generated character.

Perl script code example to create temporary directory-tempdir()

Here is a perl script example how to create temporary directory on the file system:

#!/usr/bin/perl
use File::Temp qw/ tempdir /;
my $perl_temporary_directory = tempdir();

Invalid command ‘PerlHandler’ – PerlHandler ModPerl::Registry

Apache complains on the restart:
/etc/init.d/apache2 restart
Forcing reload of web server (apache2)…Syntax error on line 48 of /etc/apache2/sites-enabled/000-default:
Invalid command ‘PerlHandler’, perhaps misspelled or defined by a module not included in the server configuration
failed!
Have a look what perlhandler it is in apache config file. In this case apache was missing : ModPerl::Registry .
Solution to this is to [...]

Debian apache problem – [error] Can’t locate Apache/DBI.pm in @INC

Debian Apache will not start and it complains in log file:
[error] Can’t load Perl module Apache::DBI for server exiting…
This is because it can not find perl module which apache will use to connect DBI database. Solution for this is to install package:
libapache-dbi-perl – Connect apache server to database via perl’s DBI
apt-get install libapache-dbi-perl

CREATE, INSERT, SELECT, UPDATE Perl script and PostgreSQL database examples

Simple perl to postgresql examples:
#!/usr/bin/perl
#load perl postgresql module
use DBI;
$postgresql_database=yourpostgresdatabase;
$postgresql_user=yourpostgresuser;
$postgresql_password=yourpostgrespass;
$postgresql_host=localhost;
# connect to perl to postgresql database
my $dbh = DBI->connect(“DBI:Pg:dbname=$postgresql_database;
host=$postgresql_host”, “$postgresql_user”, “$postgresql_password”);
# EXAMPLE PERL and POSTGRESQL
# CREATE TABLE EXAMPLE
$sth = $dbh->prepare(“CREATE TABLE perl_postgres
( language varchar(40), version integer);”);
$sth->execute();
# EXAMPLE PERL and POSTGRESQL
# INSERT DATA INTO TABLE
$dbh->do(“INSERT INTO perl_postgres(language, version)
VALUES (?, ?)”, undef, (‘perl’, 5)) or die (“Cannot INSERT”);
# [...]

Match / validate an email address with Perl regex ( regular expression )

Match or validate an email address with perl regex ( regular expression )
This perl script shows how to validate an email address using perl and regular expression ( regex ).
#!/usr/bin/perl
@email = (“email\@address”,”email\@address.email”,
“\@address.email”,”address.email\@email”, “validate\@email.address”);
foreach (@email) {
if (/^[A-z0-9_\-]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{2,4}$/) {
print “$_\n”;
}
}
Result:
email@address.email
validate@email.address

Convert from mp3 to ogg and from ogg to mp3 format with soundconvert

First install soundconvert linux package. This allows you to convert between ogg and mp3 sound media formats. On debian or ubuntu simply:
apt-get install soundconvert
to convert from ogg format to mp3 format use command:
soundconvert.pl -o mp3 mediamusic.ogg
to convert from mp3 format to ogg format use command:
soundconvert.pl -o ogg mediamusic.mp3
If you wish to convert all files [...]

Fibonacci number in while loop

Using single while loop to calculate Fibonacci number. This script is in Perl but can be easily translated to any other language:
#!/usr/bin/perl
$i=0;
$fibonacci_number=0;
$a=1;
$b=0;
while ($i < 30) {
$b = $fibonacci_number;
$fibonacci_number = $a + $b;
print $fibonacci_number . “,”;
$a = $b;
$i++;
}
print “\n”;
Output:
./fibonacci.pl
1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,
10946,17711,28657,46368,75025,121393,196418,317811,514229,832040,