FAGZAL's blog

Channels : ALL    Perl 5  (2)   PET  (2)   Misc.  (1)   Perl-2-PHP  (1)  
2008, Január 21., 19 h

This subtitle is for PHP programmers who wish to learn Perl. I decided to write it because in theory PHP and Perl are both script languages which have a lot in common, yet there are many PHP programmers who find Perl difficult, if not impossible to learn. (Interestingly this is usually not true vica-vesa.)

I assume:
- you know PHP well
- you do not know Perl, but wish to learn it
- you have access to a computer where you can run the Perl interpreter

Most of the time I will be comparing PHP constructs with theis Perl equivalents. Later it's going to be about Perl constructs and features that you do not find in PHP. Enjoy!

 

Comments are switched off for this entry

We had a hosting server which ran two instances of HTTPDs: one for administration purposes and one for the websites we host. Recently this server was upgraded from a very old Redhat 8 to Fedora (Core) 7. I have recreated many of the configs, and run into some trouble setting up those two instances of Apache (actually Apache 2.2).

(FYI, the two instances are needed for security and performance reasons: the admin server loads mod_perl and some other big modules, running as a normal user, while the httpd for our users have nobody privileges and it had mod_php and a few other small modules loaded. As the matter of fact I have yet another webserver as a reverse proxy on this machine, but let's not go into that now.)

Anyway, the trouble was that when I tried to shut down one of the servers, it took down the other one as well. I had two separate configurations, different DocumentRoots, PID files, and so on.

Without going into too much details, I have tracked down this problem to two things. First is the init script that comes which Fedora. Line 74, in /etc/init.d/httpd. See here:

stop() {
        echo -n $"Stopping $prog: "
        killproc -d 10 $httpd
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}


Line 74 is this:
killproc -d 10 $httpd

Now this calls killproc as supplied by /etc/rc.d/init.d/functions, and it actually kills the process by name - at least as fas as I can tell (I am not a BASH guru).
Change this to:
killproc -p $pidfile -d 10

And bingo! Of course you have to make sure $pidfile is set.

The other problem is "mea culpa": I have copied /etc/httpd to another location, and I used a pid file pointing to run/httpd.pid in both of the servers. Now the "run" directory is actually a symlink to /var/run, so the two pid files overwrote each other! When given another name, everything started working.

Here is a short description on how to create two (or more) instances of the same httpd (Apache) binary running on a Linux (in our case: Fedora) server!

1. You have /etc/httpd as your Apache config dir and /etc/init.d/httpd as your init script for the "original" apache.
2. Make a copy of the dir /etc/httpd. Let's call it now /etc/httpd2
3. Make a copy of the init script /etc/init.d/httpd as /etc/init.d/httpd2
4. Make a copy of /etc/sysconfig/httpd as /etc/sysconfig/httpd2
4. Configure /etc/httpd2/conf/httpd.conf such as
- the ServerRoot points to /etc/httpd2
- PidFile is set to runt/httpd2.pid
- port:IP pairs do not overlap with those set with your "original" httpd configuration
5. Modify /etc/init.d/httpd2 such as
- "killproc -d 10 $httpd" is "killproc -p $httpd" (appears twice in the file!)
- do this in /etc/init.d/httpd, too
- instead of /etc/sysconfig/httpd load /etc/sysconfig/httpd2
6. Change /etc/sysconfig/httpd2 so that it contains:
- OPTIONS='-d /etc/httpd2'
- PIDFILE=/var/run/httpd2.pid
- LOCKFILE=/var/lock/subsys/httpd2
- CONFFILE=/etc/httpd2/conf/httpd.conf

And that's it!

Make sure you first take down Apache (if it's running) before you modify all the config files. Then start the two instances one-by-one.
If the second Apache instance is not starting up, claiming it cannot bind to the port(s), then you have either overlapping ports in your httpd.conf -s, or somehow you are loading wrong configs.

Of course this is for Fedora 7, for Apache 2.2 installed as a binary (RPM) package, so YMMV. Use this at your own risk!
 

Number of comments:0
2007, November 19., 23 h

The task: get an image file, and resize it so that the resulting image (thumbnail) fits in a predefined rectangle without its proportions distorted. We should be able to handle as many image formats as possible.

Without much ado, let's see the code first.

Basic Perl Thumbnail Generator

use strict;      
use Image::Magick;   

# define dimensions, the source and destionation files
my ($width$height$infile$outfile) = (160,160'./source.jpg'
   './thumb.png');

# create object, read source image
my $im = Image::Magick->new;
my $read = $im->Read($infile);
$read && die($read);

# get ORIGINAL width and height
my ($w$h) = $im->Get('width','height');
print "Read: $infile. Dimensions: $w x $h\n";

# Find proportions, calcluate new width and height
my ($neww$newh);
if ($w/$h > $width/$height) {
   $neww = $width;
   $newh = int($h * ($neww/$w));
else {
   $newh = $height;
   $neww = int($w * ($newh/$h));
}
print "New size: $neww x $newh\n";

# Scale image, write out
$im->Resize('width' => $neww'height' => $newh);
$im->Write($outfile);
print "Done.\n";


This is a very much simplified code with a minimal set of "features". We use an image manipulator module called ImageMagick, which is one of the best free image libraries (it's a library written in C, the CPAN module we use is "only" a module to bind Perl functions to C functions).

We read in the image, calculate the new dimensions, then resize and write out the resultin image. We resize even whe it is not mandatory, i.e. if the source image is actually smaller then the one we create.

Note: AFAIK ImageMagick can create a thumbnail with one function call. Let's not worry about that now
 

Number of comments:0
2007, November 19., 20 h

This subtitle is about the Perl (Perl 5) programming language, more precisely about some typical tasks one encounters while programming in Perl.
More »

Number of comments:0
2007, November 17., 0 h

This subtitle is about PET, a Perl application server and RAD environment for web programming. It is similar in many concepts to Tomcat for Java, Rails for Ruby or Zope for Python. There is also a resemblance with Apache::ASP or Catalyst.

PET uses the excellent TT2 as its templating engine, and is built upon a few gazillion CPAN modules. It can be run in different ways, but in production, it is usually run as a daemon, and must be coupled with a webserver that has FastCGI connectivity.

Currently PET only supports Linux. Please see some incomplete and outdated docs for more info.

I am going to include some actual trick and examples here, the purpose being to show what you can achieve using PET.

 

Comments are switched off for this entry

A very typical web programming task is to take a directory which contains a bunch of images, and create a thumbnail gallery out of it. The Album module does this very professionally, and an actual industry - TGP sites - is built around this idea.

In theory, it is relatively easy to create an album. All you have to do is to list the directory, get the images, resize them and then display the thumbnails on a (HTML) page with links to the original images. So let's make this easey in practice, too!

Using a few PET built-ins, I am going to show you how to create an album without writing any Perl code - using only 6 lines of code. We will have a page list.pet which lists the thumbnails, and another one called show.pet, which shows the big (original) image.

list.pet
<io:getDir=images dir="html/images" exti="jpg|jpeg" />
[% FOREACH image = images %]
 <a href="/show.pet?file=images/${image.file}"><image:thumbnail 
   maxwidth=
"100" maxheight="100"  file=image.path border="0" /></a>
[% END %]


show.pet
<img src="/${Query.file}" />


That's it! Of course this has no extra HTML, so if you want to make it look nice, you have to put in some CSS & DIVs & TABLEs and whatnot, but this is all the codig you need.

Now let's go into the details!

More »

Number of comments:0


See GALLERY»

1. Perl-2-PHP
   2008, Január 21., 19h
2. Running multiple instances of Apahce (httpd) on Fedora
   2007, December 14., 21h
3. First recipe - creating thumbnails
   2007, November 19., 23h
4. Perl blog - what's this?
   2007, November 19., 20h
5. PET tricks
   2007, November 17., 0h
6. Creating a TGP-like gallery using PET in 6 lines
   2007, November 17., 0h

Archive
rss newsfeed icon
Advertisement

Title: Stacks in the Bükk
Visited: 157
Uploaded at: 2003-03-12 04:02:01
Zsigmond Pálnagy oil painting
Title: Loading in the Field
Visited: 482
Uploaded at: 2003-03-12 04:17:41
Zsigmond Pálnagy oil painting
Title: Washing
Visited: 113
Uploaded at: 2003-03-12 03:56:49
Zsigmond Pálnagy oil painting
Title: Oilpress
Visited: 191
Uploaded at: 2003-03-12 04:04:35
Zsigmond Pálnagy oil painting
Title: Hungarian Advent
Visited: 184
Uploaded at: 2003-03-11 07:53:02
Zsigmond Pálnagy oil painting
Title: Self Portrait
Visited: 109
Uploaded at: 2003-03-12 03:55:57
Zsigmond Pálnagy oil painting
Title: Fog in the Fall
Visited: 146
Uploaded at: 2003-03-12 04:03:14
Zsigmond Pálnagy oil painting
Title: Hungarian Farm
Visited: 206
Uploaded at: 2003-03-12 04:02:42
Zsigmond Pálnagy oil painting
Title: Part of a Farmyard
Visited: 98
Uploaded at: 2003-03-12 04:00:03
Zsigmond Pálnagy oil painting
Title: Corn Loading II
Visited: 157
Uploaded at: 2003-03-12 04:03:57
Zsigmond Pálnagy oil painting
Title: Harvest
Visited: 316
Uploaded at: 2003-03-11 07:54:01
Zsigmond Pálnagy oil painting
Title: Deep Valley
Visited: 150
Uploaded at: 2003-03-12 04:00:54
Zsigmond Pálnagy oil painting