Tuesday, April 29, 2008

Perl - How to merge 2 simple hashes ?

How to merge 2 hashes ? with the use of hash slice:

my %hash1 = ( hello1 => 'world1' , foo1 => 'bar1', hello2 => 'foobar' );
my %hash2 = ( hello2 => 'world2' , foo2 => 'bar2' );
my %hash = %hash1; # %hash contains the same keys/values as hash1
@hash{keys %hash2} = values %hash2; # We merge the content of hash2 with hash


This uses hash slices in order to merge the content of hashes. Slices can also be used to remove duplicates entries in an array:

my @array = qw/hello world foo bar hello/;
my %hash;
@hash{@array} = undef;
my @array = keys(%hash);

Friday, April 11, 2008

Transforming Unicode

I used to work in the localisation industry (terrible world).
Once, a former colleague asked me if it would be possible to retrieve the japanese characters from the unicode string. With Perl, the answer is "YES"
Just make sure you have the module Unicode::Escape installed, save your unicode string in a text file (i.e. unicodetext.txt) and run the oneliner:

perl -MUnicode::Escape -ne 'print Unicode::Escape::unescape($_);'/path/to/unicodetext.txt

Example with some strings in russian:
With a unicode string:

\u041f\u0440\u0435\u0434\u0438\u0441\u043b\u043e\u0432\u0438\u0435


the result would be:

Предисловие

Your favorite Linux command

I am subscribed on a french Linux mailing list, lea-linux.org and one of the moderator, Emmanuel F., sent this nice command to find out what your favorite command is:

history|awk '{a[$2]++ }END{for(i in a){print a[i] " " i}}'|sort -rn|head

Tuesday, April 8, 2008

modperl - PerlAuthenHandler

Let's say you are working on tenth of web services. All of them work perfectly.
And one day, your boss comes to you and says: "I need you to implement an Authorization mechanism for every web services".
How do you think you can do that?
You can create a class and edit every single handlers (web services) to handle authentication at the application level.
or
You can use the power of modperl and do this at the server level.

Apache runs several phases (some depends on your configuration) before running the actual code you want to serve (called the response handler).
Those phases can be found here

The interesting phase (or HTTP handler) for authentication is PerlAuthenHandler.

A basic configuration would be :

<Location /mywebservice/>
##############
# The below directives are mandatory in order
# to step in the PerlAuthenHandler phase
AuthType Basic
AuthName Test
Require valid-user
##############
SetHandler perl-script
PerlAuthenHandler MyAuthenticationModule
PerlResponseHandler MyWebService
</Location>


Only the "Basic" type of authentication is supported (at the moment?)
When running this phase, your favorite web browser would pop up a login window.
You would then have to implement your authentication mechanism in the handler set at PerlAuthenHandler (in our case MyAuthenticationModule).

The description of the PerlAuthenHandler is quite straight forward.

This type of Authentication is called "Basic HTTP Authentication". After successful authentication, for every requests your browser will send, the HTTP request header will contain a "Authorization" field containing your credentials (username/password) encrypted with an base-64 algorithm.
To reset the credentials, the server will have to call $r->note_basic_auth_failure and return a status code Apache2::Const::HTTP_UNAUTHORIZED.

If you want a fancy web interface to deal with Basic HTTP authentication, my advise is to forget about it as there is no way to reset credentials (if you want to simulate a timeout session) except with the method above, but you will always get the login window ...

So all you have to do is to edit the Apache configuration file and include the above directives without touching any code of your web services.

modpython also has access to HTTP handlers. It uses the same name as modperl for configuration directives (smart move). I m not sure if php has this feature of using Apache HTTP handlers, to investigate...

Monday, April 7, 2008

What is poti marara?

What is a "poti marara"?
"Poti marara" is a polynesian speed boat used for fishing.
The boat is drivable by a stick. Like this, the fisherman can drive the boat with one hand and hold in the other hand his harpoon or net.

A photo and a more-detailed description can be found here

Why did I choose this name?
Because I like it.

Introduction

No, "New Kids On The Blog" is not a blog about a boy band from the 80s-90s.
It's my (Cyril Cheneson) blog. Here, I' ll talk mainly about stuff I am working on (Apache, modperl, LDAP...), but also about anything.

See a short biography:
I am Cyril. I was borned in a tiny island lost in the middle of the Pacific Ocean called Tahiti which is part of the French Polynesia. No, we are not savage, we do have clothes and we do speak french.

I grew up there and moved to France at around 15-16 years old.
After getting my leaving certificate (baccalaureat) with a speciality in electronics, I continue my studies in M.I.T.(Montpellier Institut of Technology) in GEII (Electronic Engineering and Industrial Computing). After those 2 years study and the diploma, I moved to Dublin,Ireland where I work as a QA engineer and Test Automation Engineer for 6 years.

Today, I live in Prague (+2.5 years), Czech Republic where I work first as a perl developper for MonsterTRAK (a division of Monster.com) and now,I work as a perl developper for a company specialized in 3 differents businesses: Video streaming for mobile phones, J2me games and web development.

I like:
  • hiking
  • inline/ice skating
  • snowboarding
  • biking (but dont have a bike at the moment)
  • travelling
  • meeting new people
  • music
  • computers
  • sports (squash, basketball ...)
I don't like
  • doing nothing at work for weeks
  • Dublin Bus
I hope I will find the time to update this blog as often as possible but most of the time, lazyness takes over motivation but stay tune.