|
|
|
It's okay to program in Perl
Baby-Talk and we won't laugh. --Programming Perl, Preface
|
| Intro | Start
| Style | Basics | Example
| Full Guide | Links |
This section is being developed bit by bit. As and when I get time to write useful
things, they will be added.
| Text processing | File cleanup |
This script takes in a file called apps.txt that contains a list in the following
format:-
Location\Application
Place\Software
Where\What
and outputs a list of the second parameter on each line in the file.
1 #! /usr/bin/perl -w
2 #
3 # opening the datafiles
4 open (OUTFILE, ">output.txt");
5 open (INFILE, "apps.txt")
6 or die "Can't open the user list file!";
7 while ()
8 {
9 (undef, $name[$i]) = split(/\\/,$_);
10 chomp $name[$i];
11 print OUTFILE "$name[$i]\n";
12 ++$i;
13 }
14 close INFILE;
15 close OUTFILE;
Deconstruction
| Line no. |
Comments |
| 1 |
calling the interpreter |
| 4 |
opening a resource;
open (FILEHANDLE, "filename")
using > to write to, and < to read from, though these are not
mandatory.
|
| 6 |
a continuation from the previous line, but this resource manipulation
will complain if it cannot open the file;
die
"TEXT" and in fact will terminate the application.
|
| 7 |
while (CONDITION) {OPERATION}
is a
command to cause a loop while a condition exists; in this case while
there are lines in the input file, and will execute the operations
within the braces until that condition is no longer met.
|
| 9 |
Assignment of values from the input file to variable arrays, using the
command
split (/PATTERN/, VARIABLE, LIMIT)
to chop up the input lines; where LIMIT is not mandatory and specifies the maximum
number of fields into which the string may be split. If the VARIABLE is
omitted, the operation is executed on the $_. If PATTERN is omitted, it
splits on whitespace.
|
| 10 |
Any trailing carriage returns and line feeds need to be tidied up from variables
filled from files or standard input. The safe way of doing this is to use
chomp VARIABLE/LIST
|
| 11 |
Outputting the results of all of these operations to a file, is affected
by the use of
print FILEHANDLE LIST
if LIST is omitted $_ is assumed. Note that if you're storing FILEHANDLES
in an array or other expression, you will have to use a block returning
its value instead:
print {$filenamess[$i]}
"stuff\n";
print {$OK ? STDOUT : STDERR} "stuff\n";
|
| 12 |
This is a standard method of incrementing an integer variable
++VARIABLE
|
| 14 |
Of course, it is a good idea to close filehadles once they're opened:-
close FILEHANDLE
|
Return to [top]
[Intro]
This looks at files in a directory and its subdirectories, at the last accessed date on
the file and if it is more than 30 days old the files gets deleted. A limitation of
using this script under Win32 conditions is that it will ask for verification of deletion
of the contents of subdirectories.
1 #! /usr/bin/perl -w
2 #
3 # MAIN
4 sub MAIN
5 {
6 use File::Find;
7 print "\n\n\n Starting directory? ";
8 $start = <stdin>;
9 chomp $start;
10 $expire = time - 108060;
11 find(\&FOUND, "$start");
12 }
13 #
14 # FOUND
15 sub FOUND
16 {
17 ($atime) = (stat("$_"))[9];
18 if ($atime lt $expire)
19 {
20 system "del $_\n";
21 }
22 }
23 &MAIN
Deconstruction
| Line no. |
Comments |
| 4 |
Declaring the subroutine name. Subroutines are enclosed in curly braces. |
| 6 |
Calling a Perl module.
use Module VERSION LIST
where Module is the Perl module name; VERSION demands that the
specified Perl version be used.
|
| 8 |
Collecting input from the standard input - in this case, the keyboard. |
| 11 |
Invoking the subroutine that performs actions on the files through which
the find module recurses, feeding in $start as the starting directory.
|
| 17 |
stat FILEHANDLE
Returns a 13-element list giving the status info for a file, either the file
opened via FILEHANDLE, or named by EXPR. If EXPR is omitted, it stats $_. Returns
a null list if the stat fails. Typically used as follows:-
($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize,
$blocks) = stat($filename);
|
| 18 |
Logic:- binary operators, which return TRUE or FALSE
Binary "<" returns true if the left argument is numerically less
than the right argument.
Binary ">" returns true if the left argument is numerically
greater than the right argument.
Binary "<=" returns true if the left argument is
numerically less than or equal to the right argument.
Binary ">=" returns true if the
left argument is numerically greater than or equal to the right argument.
Binary "lt"
returns true if the left argument is stringwise less than the right argument.
Binary
"gt" returns true if the left argument is stringwise greater than the right argument.
Binary "le" returns true if the left argument is stringwise less than or equal to the right
argument.
Binary "ge" returns true if the left argument is stringwise greater than or
equal to the right argument.
|
| 20 |
Commands to be executed in the operating system can be called as follows:-
system "COMMAND"
|
| 23 |
Subroutines are called by name. If variables are to be passed to and from the
subroutine, add a &before the subroutine name when calling it.
|
Mailto:webmaster@totkat.org
Site statistics
|