Data serialization

I recently needed a reliable way to pass binary information between two processes through an IPC mechanism. One of the programs (P1) consumes data from a DB2 database which is kinda complex to work with when the code is written in C, so, I ended up looking for a way to remove this (unnecessary) complexity by writing a Perl (P2) script much more user friendly when comes to working with database connections.

The query was as simple as

which means that a simple C structure would be enough to map the entire selected fields

P1 works as a TCP/IP server listening at port 7007. P2 connects to that endpoint, executes the query and packs the resulting row as a binary data packet to finally send it through the opened socket. P1 deserializes the bytes received to build the structure rem_data.

P1 is possibly long but self-explanatory :).

Compile it with

P2 is more than simple. In case you don’t know the pack() function, here is very well explained.

And then, we just run it.

And P1’s output should be

Garbage generator

When your teacher asks you to write a script that randomly mixes grammatical parts of a sentence because you didn’t did your homework is what causes this kind of post.

Actually there were more restrictions. I had to do it in less than 10 minutes, the resulting sentence needed to be understandable despite the possible genre errors and finally, the sentence should be generated in Spanish.

The script is made in Perl. It was easy to build and luckily took me less than 10 minutes to entirely write both the code and the feeders (the files with the words to build the sentence).

[perl]
#!/usr/bin/perl

use warnings;
use strict;

sub main()
{
my @articles = open_file($ARGV[0]);
my @subjects = open_file($ARGV[1]);
my @verbs = open_file($ARGV[2]);
my @complements = open_file($ARGV[3]);

my %rules = ( ‘a’ => @articles,
‘s’ => @subjects,
‘v’ => @verbs,
‘c’ => @complements);

my @expr = split(/+/, $ARGV[4]);

for my $token ( @expr )
{
my $index = int(rand(scalar(@{$rules{$token}})));

my $word = @{$rules{$token}}[$index];
$word =~ s/n+/ /g;
print $word;
}

print “n”;

}

sub open_file($)
{
my $file = shift;

open(FILE, “< $file") || die "Error: $!n"; return ;
}

&main();
[/perl]

The program receives 5 parameters from command line: the articles file, the subjects file, the verbs file, the complements file and the expression which determines how to build the sentences combining the words.

[bash]

$ perl random-sentence.pl articles subjects verbs complements “a+s+v+c”

[/bash]

The above call produces mostly nonsense and funny sentences like:

– el edificio duerme todo al pedo
– un alumno estudia como la mierda
– la puerta rie
– las perro habla todo al pedo
– una profesor come cualquier cosa

It was a funny thing to do. I don’t know if the script worth a post in my blog but here it is and I laughed a lot 😀

New domain, new stuff to write, new language

My mother language is Spanish but everything related to my career as a computer engineer is written in English so there is an obvious need to learn to speak English and, this is me writing my very first post completely in English.

Eventually I’ll become more fluent and I will stop making grammatical errors but until that happens, I ask you to be patient because I’m pretty sure the main idea of what I’m trying to say will be very understandable, like this paragraph for instance 😀

I’m working to upload my projects to github. Those projects are going to get published here. The idea is to share what I learned all these years of playing around with computers and software.

Well, that’s it 🙂