On Thu, Mar 19, 2009 at 06:11, Raheel Hassan <raheel.has...@gmail.com> wrote:
> Can anybody explains this piece of code, i have difficulties in
> understanding it,
>
>
> my $errorlog = "/var/log/controler.log";
>
> &initLanguage($language);

No idea what this does because there is no context for it.

> &launchCbox();

This runs the code below.

> sub launchCbox
> {
>
>
>        $scr = Term::ScreenColor->new;

This creates a new Term::ScreenColor[1] object called $scr.
Term::ScreenColor allows you to position text on the screen and
control its color.

>         $scr->clrscr();

here we are calling the clrscr method of the $scr object.  This causes
the screen to be cleared.

>        my ($mode) = @_;

This attempts to get the first argument passed to this function, but
since you didn't pass any $mode will be undefined.

>        my $error = 0;

Sets a variable named $error to zero.

>         my $cbox_x = $dog_x + 3;

sets a variable named $cbox_x to the value of $dog_x (which is created
and set somewhere else) plus three.  If I had to guess, I would say
that $dog_x was used to draw a some form of dialog box, and we are
trying to position this cbox inside of the dialog box.

>
>         my $args;

This creates a variable named $args.

>         my %box_args = %{$args[3]};

This creates a hash named %box_args out of the fourth item in @args
which is set somewhere else.  A hash is a set of key/value pairs.  You
can access a value in the set by saying $box_args{key}.

>         $args .= "-d " if $box_args{"debug"};
>         $args .= "-l " if $box_args{"log"};
>        $args .= "-L ".$box_args{"language"} if $box_args{"language"};

here we are building a string inside of $args.  If the value in
%box_args associated with key key named debug is true then we add a
"-d " to the string.  The same goes for the others.

>
>        if($mode ne "restart")

The code inside of {} after this statement will execute if $mode is
not the string "restart".  Since we know that $mode is empty (see
above), we know this code will execute.

> { $scr->at($y_pos,3)->puts($x.".
> ".$txth->getText("5016")); }

This prints the value of $x (set somewhere else) concatenated with "."
concatenated with the value of calling the getText method of $txth
(set somewhere else) with the string "5016" at the position on the
screen $y_pos (set somewhere else), 3.

If I had to guess, I would bet that $txth is an object that holds a
bunch of strings that have been translated into a bunch of different
languages, and that you are asking for the string associated with
5016.  This is a common technique when working with applications that
will be run in many countries.

>         else { $y_pos = $dog_y_pos+3;
> $scr->at($dog_y_pos+3,3)->puts($cbox_x.". ".$txth->getText("5016"."
> "x($status_bar_pos-(length($txth->getText("5016"))+7)))); }

This does something similar to the code above if the value of $mode
was "restart".

>
>        if(!$xwindow)

Similar to the code above, if the $xwindow variable (set somewhere
else is not true, the code in the braces below will execute.

If I had to guess, I would bet that this is a flag that tells us if we
are running under the X Windowing System or not.

> { `perl ./cbox.pl 2>>/var/log/cbox.log $args >
> /dev/tty9`; }

This code runs another perl program named cbox.pl in the current
directory.  The program is passed a command line options that we built
above (e.g. -d if debug was set).  It is redirecting error messages to
a log file and writing its STDOUT to /dev/tty9 (which is a terminal).
My bet is that this program draws the rest of the cbox.

>         else { `perl ./cbox.pl 2>>/var/log/cbox.log $args > /dev/pts/2`; }

This code executes if the value in $xwindows is true, and it does a
similar thing, but writes to /dev/pts/2 (which is a pseudoterminal).
This lends support to my theory that $xwindows is a flag that tells us
if we are running X.

>         $error ? &printNOK : &printOK;

Unless I am missing something $error is never changed from its
original value of 0, so the function printOK will always be called.
Given the odd nature of $error (false being good) I am betting that
the original intent was to use the system[2] function instead of the
qx// operator[3] (the `s above) to execute cbox.pl and the person who
wrote this intended to store the return value from system in $error.

All in all, that was some horrible code.  Bad indenting style, global
variables, calling another Perl program instead of writing a module to
do common functions, use of negative comparisons in if statements that
have else clauses, and Perl 4 style function calls.  My bet is that
this code was written by someone who knew ANSI/ISO C fairly well and
knew very little Perl.  The obvious hack of using the qx// operator
where a call to the system function would have been more appropriate
speaks of someone who knew roughly what he or she wanted to do, but
was struggling to find a way to make it work.

1. http://search.cpan.org/dist/Term-ScreenColor/ScreenColor.pm
2. http://perldoc.perl.org/functions/system.html
3. http://perldoc.perl.org/perlop.html#qx/STRING/

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to