Perl
Perl
Summary ...
Contents
Introductory reading
Perl
Perl is a programming language.
perl is actually a program that runs commands in the Perl programming language. But from a user's perspective, that really doesn't make a difference.
Perl is:
- free-format (whitespace is optional)
- compiled (everything is looked at before its executed)
- interpreted (works from code, step by step)
... with
- automatic typing and automatic memory management.
What is Perl good for?
- Text processing
- Rapid prototyping
- Easy to learn for easy tasks
- Powerful enough for difficult tasks
- Programming for the Web
- Use of large libraries of useful code modules
- "Magic"
- TIMTOWTDI - "There's more than one way to do it".
What is Perl bad at?
- Need for complex datastructures
- Performance-critical applications
- Complex, long-lived software projects with multiple authors
- "Magic"
- TIMTOWTDI - "There's more than one way to do it".
More information on Perl
... to be found here. No static list would make any sense.
cat in Perl
print()
The print function takes a list as it's argument and writes the list to STDOUT or any other designated "filehandle".
print List;
I have installed GeSHi, a syntax highlighter, on this Wiki. Source code is colored so that the different semantics of its elements can be easily seen.
There are three points to make:
(1) Its use is straightforward in principle: list elements are passed to the function, evaluated and written to file.
print("Line $number: ", $line, "\n");
(2) Any filehandle can be used as target (STDOUT is default):
open(OUTFILE, ">output.txt");
print(OUTFILE "$value\n";
(3) Variables are interpolated but not evaluated:
my $i = 4;
print("value: $i**2\n"); # value: 4**2
print("value: ", $i**2, "\n"); # value: 16
The string "\n" denotes a linefeed. It uses the "escape character to either prevent interpolation of special characters or to create special characters from normal ones.
" \n " # newline
" \b " # backspace
" \t " # tab
" \\ " # the backslash itself
" \" " # a double quote
" \$ " # a $ sign (variable is not interpolated)
my $i = 42;
print ("\$i is $i\n"); # $i is 42
a cat program
Create a new file by typing
nano cat.pl
nano or pico are simple text editors, installed on most Unix systems.
Enter the text below exactly as it is written here. Don't mix up the bracket types.
Save by typing <ctrl>o, and exit <ctrl>x.
Make the file executable:
chmod u+x cat.pl
or
chmod 700 cat.pl
Here is the code:
#!/usr/bin/perl
use warnings;
use strict;
while (my $line = <STDIN>) {
print($line);
}
exit();
Save the file and run the code. Type something and press return. When you are all done, type <ctrl>d to signal the end of input. The program should behave exactly like the cat command. But now we have all single steps of the program at our fingertips, to change and modify them as we wish.
use "strict" and "warnings"
Warnings tell you when Perl thinks you typed things you didn't even mean.
Strict makes Perl complain when variables were not declared - like when they have been mistyped !
Try the error checking functionality:
Replace $line
by $lime
behind the print statement,
run the program.
"Comment out" use warnings
(put a '#
' before it), then
change =
to ==
. Run. Abort with <ctrl>c
when you are fed up. Remove the #
and run this again.
Installation of new modules
How to install Perl modules and programs on Unix systems.
Generic installation
- 1. download the source-archive
- 2. unzip and untar the file
- 3. cd to the newly created directory
- 4. type Makefile.PL
- 5. type make
- 6. type make test
and if the test results appear reasonable:
- 7. type sudo make install
Installation via CPAN
sudo perl -MCPAN -e "install ''My::Modules''"
or
sudo perl -MCPAN -e "force install ''My::Modules''"
Further reading and resources