≡ Menu


Installing Apache, PHP and mod_ssl on MS Windows

Q. How do I install latest version of Apache, PHP and mod_ssl for testing purpose in Windows Vista or Windows XP computer locally?
[click to continue…]

Linux Blog Software

Q. I've Debian Linux VPS server for my small business web site. Can you tell me more about a free blog software (weblog software) that can run under LAMP?
[click to continue…]

Bash Shell: Replace a String With Another String In All Files Using sed and Perl -pie Options

How do I replace a string with another string in all files? For example, ~/foo directory has 100s of text file and I'd like to find out xyz string and replace with abc. I'd like to use sed or any other tool to replace all occurrence of the word.
[click to continue…]

Linux: Find my IP address using Perl at a shell prompt

Q. How do I find out my IP address assigned to eth0 or ra0 interface using perl?

A. If you need to know the IP address of the UNIX / Linux machine you are running on, use the following perl one liner. Perl don't have any inbuilt facility but combination of ifconfig command ans shell pipes you can craft something as follows to display your system IP address:

Find my IP address using Perl One liner and shell pipes

Type the following command at a shell prompt:

ifconfig -a | perl -ne 'if ( m/^\s*inet (?:addr:)?([\d.]+).*?cast/ ) { print qq($1\n); exit 0; }'

FreeBSD install Perl language

Q. I need Perl programming language to run scripts. How do I install perl on FreeBSD operating system?

A. Practical Extraction and Report Language or Perl can be installed using FreeBSD ports or binary package system. Perl is a language optimized for scanning arbitrary text files, extracting information from those text files, and printing reports based on that information. It's also a good language for many system management tasks. The language is intended to be practical (easy to use, efficient, complete) rather than beautiful (tiny, elegant, minimal).

Install perl using FreeBSD ports

Perl5 port is located at /usr/ports/lang/perl5.8. Type the command as follows to install Perl5:
# cd /usr/ports/lang/perl5.8
# make;make install;make clean

Now perl will be installed and binary path to perl is /usr/local/bin/perl.

Install perl using FreeBSD pkg_add command

Just type the following command:
# pkg_add -v -r perl

Howto: Write script in Perl

Q. I’m new to Linux and Perl. I’ve printed perl man page but it is bit confusing and omits a lot of simple things or details. Can you tell me how do I write a perl script? How do I open perl editor?

A. Larry Wall began work on Perl in 1987 and it is a dynamic programming language. Traditionally perl programs are written using text editor such as vi or emacs. The overall structure of Perl derives broadly from C.

Also when you start learning a new programming language, always start with Hello world program.

Hello world Perl Program

Let us print Hello world from a shell prompt. Type the following command (excluding $ ):
$ perl -e 'print "Hello, world!\n"';

Hello, world!

Let us write hello world program using vi text editor:
$ vi hello.pl
Append code as follows:
print "Hello, world!\n";

Save and close the file. Now setup a execute permission:
$ chmod +x hello.pl
Finally execute perl program:
$ ./hello.pl

  • First, I used the vi command to create a file named hello.pl
  • The first line of the script used to specify that the script is to be executed by perl program (#!/usr/bin/perl) and not by a shell.
  • Print command prints hello world on screen. Please note that the notation \n which stands for newline i.e. print a newline.

Further reading

This is just a simple introduction. You should consider following text books & resources for more information and mastering the perl:

FreeBSD find out RAM size Including Total Amount of Free and Used Memory Size

How do I find out RAM size or memory size installed in my FreeBSD server? How do I display amount of free and used memory in the system powered by FreeBSD? How do I find out how much RAM is installed on a FreeBSD 10 server?
[click to continue…]

Display or view the perl cgi errors in a web browser

Q. I’m currently learning Perl cgi programming and coded a small web site in a Perl. Usually errors are logged in a log file. How can I view the perl cgi errors in a we browser like firefox?

A. CGI scripts have a nasty habit of leaving warning messages in the error logs that are neither time stamped nor fully identified. Tracking down the script that caused the error is a pain.

You need to install and use CGI::Carp module.

With this module the standard warn(), die (), croak(), confess() and carp() calls will automatically be replaced with functions that write out nicely time-stamped messages to the HTTP server error log.

You can also log message to a browser. Now add following two lines before sending any headers to a browser:

use CGI;
use CGI::Carp qw(warningsToBrowser fatalsToBrowser); 

With above lines it is possible to make non-fatal errors appear as HTML comments embedded in the output of your program. To enable this feature, export the new "warningsToBrowser" subroutine. Since sending warnings to the browser before the HTTP headers have been sent would cause an error, any warnings are stored in an internal buffer until you call the warningsToBrowser() subroutine with a true argument.

See official CGI::Carp man page for more information.

Perl Display And Pass Command Line Arguments With @argv

How do I read or display command-line arguments with Perl?
[click to continue…]

Perl sprintf: How Do I Use sprintf In a Perl Script?

How do I use sprintf() function in a perl script under Linux or UNIX?
[click to continue…]