Feb 27, 2012

A simple PERL script

PERL has become one of the most popular server scripting language. Because most of my experience is with administering networked machines I decided that I had to learn a little about it.

A quick overview of the program:
  • Designed for Linux!!! Need to edit the code to make it work for other operating systems
  • Has one subroutine that opens a new file to write to and sets this file to the internal global variable MYOUTFILE
  • The subroutine also inserts the HTML and <title> tags
  • Uses both forms of print; printing to terminal and printing to a file handle (MYOUTFILE)
  • Storing of local variables
  • Uses exec to perform system commands (in this instance it lists the files in the current working directory)

Below is my first ever PERL program, completed in about 2010.

#!/usr/bin/perl
#
# Make PERL less forgiving and more robust
use strict;
use warnings;
#
# Creates a new HTML report
#
# INPUT: The location ($_[0]) and name ($_[1]) of the file you want to write to
sub openFile
{
    my $output = '~/' . $_[0];
    open(MYOUTFILE, ">$output") or die("Error");
    print MYOUTFILE "<html>\n<title>$_[1]</title>\n<body>\n";
}
#
# Using my to declare variables to be local
my $variable1 = "~/";
#
# Output
print("This program outlines some of perl's functionality\n");
#
# Write a new output file
openFile("network.html", "Files");
#
# Executes a system function
exec "ls $variable1"

No comments:

Post a Comment

Thanks for contributing!! Try to keep on topic and please avoid flame wars!!