Perl have a great support for parallel execution since version 5.6. Shared variables guarantees you to modify the state at the same time from different threads, without the internal state of the variable will become corrupted.
#!/usr/bin/perl
use strict;
use threads;
use threads::shared;
$|=1; # autoflush on
my $running = 1;
share $running;
print "Press enter to stop\n";
my $job = async {
while($running > 0) {
print ".";
sleep 1;
}
};
my $enter= <STDIN>;
$running = 0;
$job->join();
Tuesday, December 23, 2008
Monday, December 15, 2008
Understanding class inheritance in Perl
By using the global variable @ISA you could extend classes in Perl. Like C++, Perl supports single and multiple class inheritance.
In this sample, class B derives from base class A.
#!/usr/bin/perl
use strict;
package A;
sub new {
my $class = shift;
my %args = @_;
In this sample, class B derives from base class A.
#!/usr/bin/perl
use strict;
package A;
sub new {
my $class = shift;
my %args = @_;
my $this = {
M1 => $args{"P1"},
M2 => $args{"P2"}
};
return bless ($this, $class);
}
package B;
our @ISA = "A";
sub new {
my $class = shift;
my %args = @_;
my $this = $class->SUPER::new(@_);
$this->{"M3"} = $args{"P3"};
return(bless($this, $class));
}
my $a = new B(P1 => "bbb", P2 => "blue", P3 => "jjj");
print($a->{'M1'} . "\n");
print($a->{'M2'} . "\n");
print($a->{'M3'} . "\n");
M1 => $args{"P1"},
M2 => $args{"P2"}
};
return bless ($this, $class);
}
package B;
our @ISA = "A";
sub new {
my $class = shift;
my %args = @_;
my $this = $class->SUPER::new(@_);
$this->{"M3"} = $args{"P3"};
return(bless($this, $class));
}
my $a = new B(P1 => "bbb", P2 => "blue", P3 => "jjj");
print($a->{'M1'} . "\n");
print($a->{'M2'} . "\n");
print($a->{'M3'} . "\n");
Wednesday, December 10, 2008
Active Server Pages with Perl
You can create a simple framework to render html pages by using templates and embedded Perl code.
The html template page
<html>
<body>
<h1><perl>localtime(time());</perl></h1>
</body>
</html>
The html rendering engine
#!/usr/bin/perl -w
use strict;
sub generateHTML {
my @pass1 = split(/<\/perl>/, shift);
my @html;
foreach my $e (@pass1) {
my @pass2 = split(/<perl>/, $e);
my $value;
push(@html, $pass2[0]);
if (scalar(@pass2) > 1) {
$value = eval($pass2[1]);
push(@html, $value);
}
}
return join("", @html);
}
The html template page
<html>
<body>
<h1><perl>localtime(time());</perl></h1>
</body>
</html>
The html rendering engine
#!/usr/bin/perl -w
use strict;
sub generateHTML {
my @pass1 = split(/<\/perl>/, shift);
my @html;
foreach my $e (@pass1) {
my @pass2 = split(/<perl>/, $e);
my $value;
push(@html, $pass2[0]);
if (scalar(@pass2) > 1) {
$value = eval($pass2[1]);
push(@html, $value);
}
}
return join("", @html);
}
Friday, December 5, 2008
Object oriented development with Perl
This sample code demonstrates how to implement operator overloading with Perl.
#!/usr/bin/perl -wuse strict;
my $n = new Number(5);
$n = $n + 1 - 2;
print "$n\n";
# The class implementation
package Number;
use overload '+' => \&add;
use overload '-' => \&subtract;
use overload '""' => \&toString;
sub new {
my $class = shift;
my $this = { size => shift };
return bless ($this, $class);
}
sub subtract {
my ($this, $v) = @_;
$this->{'size'} -= $v;
return $this;
}
sub add {
my ($this, $v) = @_;
$this->{'size'} += $v;
return $this;
}
sub toString {
my ($this) = @_;
return "Summary " . $this->{'size'};
}
use overload '-' => \&subtract;
use overload '""' => \&toString;
sub new {
my $class = shift;
my $this = { size => shift };
return bless ($this, $class);
}
sub subtract {
my ($this, $v) = @_;
$this->{'size'} -= $v;
return $this;
}
sub add {
my ($this, $v) = @_;
$this->{'size'} += $v;
return $this;
}
sub toString {
my ($this) = @_;
return "Summary " . $this->{'size'};
}
Subscribe to:
Posts (Atom)