Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Jan 162010
 

Playing with qw() function in Perl

Answer:

The qw (quote word) function is very useful to generate a list of words.

E.g.

my @names = ('peter', 'john', 'mark');

print @names;

my @names2 = qw(peter john mark);

print @names2;

As you can see above, the use of qw() made the code more readable.

Jan 152010
 

Sending Apache log to syslog?

Answer:

In the Apache config (httpd.conf), add/modify the followings as needed

1. For Access log

CustomLog |/usr/local/apache/bin/apache_syslog combined

Where apache_syslog is a Perl script


#!/usr/bin/perl
use Sys::Syslog qw( :DEFAULT setlogsock );

setlogsock('unix');
openlog('apache', 'cons', 'pid', 'local2');

while ($log = ) {
            syslog('notice', $log);
}
closelog

2. For Error log

Apache already has direct support error log to syslog, so just add the target syslog facility.

ErrorLog syslog:local1

This tells Apache to send the error log output to the syslog facility local1

Jan 132010
 

Read command line arguments in Perl

Answer:

To read the command line arguments in Perl, you need to play with the special array $ARGV

E.g. test.pl

#!/usr/bin/perl
use strict;

print $ARGV[0];

Then execute

# perl test.pl hello
hello

The first argument will be printed to the screen.