Saturday, September 4, 2010

Perl - Parent and child process

Creating a child process in Perl can be done with the fork command, Sample of the command :
defined ($pid = fork) or die "Cannot create thread";

By using the $pid value we can determine if we currently in the parent process or the child process, If its 0, We are currently in the child process, else, its the parent.

A Quick sample to demonstrate it:
my $pid;
print "
Parent PID is : $$ \n";

defined ($pid = fork) or die "Cannot create process";
if ($pid == 0)
{
print "
Child process Starting with pid : $$ \n";
}
else
{
print "
Parent process Continue with pid : $$ \n";
}
print "
Current PID is $$ (pid=$pid) \n";

The output of the previous script will be (I colored the Parent and the Child outputs):

Parent PID is : 2644

Parent process Continue with pid : 2644

Current PID is 2644 (pid=2645)

Child process Starting with pid : 2645

Current PID is 2645 (pid=0)

Note : $$ is the predefined name for the current running process.

Saturday, August 21, 2010

VIM as Perl IDE

In order to use vim editor as Perl IDE, you should use the Perl-Support Vim plug-in,

This plug-in (with installation instructions) can be found at http://www.vim.org/scripts/script.php?script_id=556.

In case of fedora,This plug-in is part of the "updates" repository (vim-perl-support).

Thursday, May 27, 2010

Fedora 12 on LG R560 laptop - HDMI

The Video worked from the beginning, As soon as I plugged the HDMI cable, I received the "Display - KDE Control Module" which help me configure the Video settings.

The Audio however, did not worked.

So in order to configure the alsa (Advanced Linux Sound Architecture), I had to install the alsa volume control:

yum install pulseaudio alsa-plugins-pulseaudio pulseaudio-esound-compat pulseaudio-libs pulseaudio-libs-glib2 pulseaudio-module-zeroconf pulseaudio-libs-zeroconf xmms-pulse pulseaudio-module-gconf wine-pulseaudio xine-lib-pulseaudio pulseaudio-utils pulseaudio-module-bluetooth gst-mixer padevchooser paman paprefs pavucontrol pavumete

Now launch "Pulse Audio volume control", In the configuration tab choose : "Digital Stereo (HDMI) Output + analog Stereo Input".

And now both Video and Audio are working :-)

Tuesday, May 4, 2010

Fedora 12 on LG R560 laptop

I installed Fedora 12 on my new Laptop (LG R560), Below is the status and the resolutions of the issues I faced:

Installation:
No problem was found, Partitions, Installation, Everything worked as expected.

Power Up:
Worked fine.

WIFI:
Didn't worked, Tried to activate the WiFi adapter (Fn + F6) with no luck, According to the Laptop spec its contain Intel WiFiLink 5300, I installed the driver (iwlwifi-5000-ucode-8.24.2.12), But with no luck.
After some research I found that I have RaLink RT2860 and not Intel, So the problem was solved by installing the
kmod-rt2860-PAE.i686 package from the rpmfusion-free-updates repository and reboot.
Now, Fn+F6 do activate the WiFi adapter and I succeeded to connect my Wireless network.

Wednesday, November 25, 2009

KDE 4.3 and Conky

While using Conky (http://korenofer.blogspot.com/2008/11/my-conky-configuration.html) with KDE 4.3 There is a background problem, instead of transparent background the Conky seems to have a black background which hide the actual desktop.
The problem is that KDE draw the wallpaper on the plasma desktop instead of the root window, When Conky try to get the background from the root window, It only get the black color.
To fix this, We can use the "feh" command which can set the background image to a specific file.
What I'm having now is the following script:
#! /bin/bash
feh --bg-scale `grep 'wallpaper=' ~/.kde/share/config/plasma-desktop-appletsrc | tail --bytes=+11`
sleep 15
conky -&


Some explanation:
The `grep 'wallpaper=' ~/.kde/share/config/plasma-desktop-appletsrc | tail --bytes=+11` command will get the current desktop picture file name.
feh --bg-scale Is used to set the background image using a file name.

And then we can call Conky (The sleep 15 is for use this script during the KDE start up,this way we make sure the desktop is up before we are trying to show the Conky output on the screen).

 

Sunday, September 6, 2009

Perl inheritance - The basics

We can implement inheritance in Perl using packages (as our "classes").
The following sample will demonstrate package inheritance:

First, Lets create our base package, in this sample, I have created a simple logger package which has a constructor and a "print" method:

# Module Name

package mylogger;
## Constructor
sub new {
  print "mylogger  :new\n";
  my $package = shift;
  return bless({}, $package);
}
## Print Method
sub print
{
    print "mylogger  :print - " . $_[1] . "\n";
}
1;

If my main application will call this module:
use mylogger;
my $myLogger = mylogger->new();
$myLogger->print("Hello world");

The Ouput will be as following:
mylogger  :new
mylogger  :print - Hello world

Lets now create a newer logger which inherit the previous one and print the message time and date:
# Module Name
package newlogger;
use mylogger;
## Inherit from mylogger
our @ISA=qw(mylogger);
sub print
{
    print "newlogger :print - " . $_[1] . " (at ". localtime(time) .  ")\n";
}

Now, If we will call the new loggers following:
my $newlogger = newlogger->new();
$newlogger->print("Hello world");

The following output will appear:
mylogger  :new
newlogger :print - Hello world (at Sun Sep  6 22:00:52 2009)

OK, What happen here?
The first module is straight-forward, Please refer to my previous entry regarding Perl modules,
The second one act as following:
When newlogger->new() is being called, Perl start looking for the "new" method in the newlogger package, Since we don't have one, Perl try to look the requested method in the packages which are listed in the @ISA list.
Since we have a "new" method in our "mylogger" package, Perl call this function and do the following:
First its print the "mylogger  :new\n"  message.
Second its return a reference (by using bless) to the inserted package, which is "newlogger".

And since the "new" function returned a "newlogger" object, When the app call the $newlogger->print method, its actually calling the new method with the time stamp.







Sunday, April 19, 2009

Creating and Using Perl modules

A little example to demonstrate how to create and use Perl module in Perl application.

First we will create the module:

The module file extension should be “pm”, and it should contains:
#MODULE_START
# Module Name
package Comm;
#######################################################################
## Module related arguments

use 5.006;
## Constructor
sub new {
    my $package = shift;
    return bless({}, $package);
}

#######################################################################3
## My Code

sub printHello()
{
    print "Hello World";
}

#MODULE_END

Now, Lets use the module in our Perl application
#APP_START
#/usr/bin/perl;
use strict;
use warnings;
# My Module
use Comm;

# Module creation
my $myModule= Comm->new;
$
myModule->printHello;

#APP_END

Few Notes about this sample:

  1. The application should know the module path.

  2. As you can see, I'm calling the module function with the module prefix (Conn->....), To avoid it we can use the “Exporter” inside the module to export its function directly,