≡ Menu

Perl

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"';
Output:

Hello, world!

Let us write hello world program using vi text editor:
$ vi hello.pl
Append code as follows:
#!/usr/bin/perl
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:

Q. How do I find out RAM size / memory size in FreeBSD? How do I display amount of free and used memory in the system?

A. To displays the total amount of free and used physical and swap memory in the system, as well as the buffers used by the kernel you need to install special script Most user use sysctal command to get all data. There is a perl script that automates everything and display back result on screen.

This script query the system through the generic sysctl interface. The sysctl utility retrieves kernel state and allows processes with appropriate privilege to set kernel state. You must have perl installed on FreeBSD.

FreeBSD command about RAM size and information

Download perl script which is written by Ralf S. Engelschall:

# fetch http://www.cyberciti.biz/files/scripts/freebsd-memory.pl.txt
# mv freebsd-memory.pl.txt /usr/local/bin/free
# chmod +x /usr/local/bin/free

Make sure perl is installed. Now to display or list total system memory usage type
$ free
Output:

SYSTEM MEMORY INFORMATION:
mem_wire:          25341952 (     24MB) [  9%] Wired: disabled for paging out
mem_active:  +     47529984 (     45MB) [ 18%] Active: recently referenced
mem_inactive:+     15605760 (     14MB) [  6%] Inactive: recently not referenced
mem_cache:   +        16384 (      0MB) [  0%] Cached: almost avail. for allocation
mem_free:    +    165556224 (    157MB) [ 65%] Free: fully available for allocation
mem_gap_vm:  +       389120 (      0MB) [  0%] Memory gap: UNKNOWN
-------------- ------------ ----------- ------
mem_all:     =    254439424 (    242MB) [100%] Total real memory managed
mem_gap_sys: +      4988928 (      4MB)        Memory gap: Kernel?!
-------------- ------------ -----------
mem_phys:    =    259428352 (    247MB)        Total real memory available
mem_gap_hw:  +      9007104 (      8MB)        Memory gap: Segment Mappings?!
-------------- ------------ -----------
mem_hw:      =    268435456 (    256MB)        Total real memory installed
SYSTEM MEMORY SUMMARY:
mem_used:          87257088 (     83MB) [ 32%] Logically used memory
mem_avail:   +    181178368 (    172MB) [ 67%] Logically available memory
-------------- ------------ ----------- ------
mem_total:   =    268435456 (    256MB) [100%] Logically total memory

Linux like free command for FreeBSD

Freecolor is a free replacement that displays free memory graphically as a bargraph. It supports the same options as free. Install freecolor, enter:
# cd /usr/ports/sysutils/freecolor
# make install clean

To see memory details, enter:
$ freecolor -m -o
Sample output:

             total       used       free     shared    buffers     cached
Mem:          4082        825       3256          0          0        117
Swap:         2048          0       2047

$ freecolor -t -m -o
Sample output:

             total       used       free     shared    buffers     cached
Mem:          4082        825       3256          0          0        117
Swap:         2048          0       2047
Total:        6130 = (     826 (used) +     5421 (free))

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.

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

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

Q. Where can I download Plesk type themes for webmin Web hosting control panel?

A. Webmin is a system configuration tool for Unix-like systems. It has a web-based interface (GUI) desktop environment independent, for configuring some of the internals of the operating system. Webmin is largely based on Perl, and is running as its own process, and web server. It usually uses TCP port 10000 for communicating, and can be configured to use SSL if OpenSSL is installed.

Download sdditional webmin themes

Webmin comes with few built in themes such as caldera and some default. There is no plesk like theme exists for Webmin. However you can download other themes from following location.
=> Third party webmin themes