Siaris
Simple Things
Syndicate: full/short
Siaris
Categories
General0
News2
Programming2
LanguageBits0
Perl50
Ruby10
VersionControl1
Misc1
Article Calendar
<= July, 2008
S M T W T F S
12345
6789101112
13141516171819
20212223242526
2728293031
Search this blog

Key links
External Blogs
Brought to you by ...
Ruby
1and1.com

Building a Simple Module by Hand, Part 1

Andrew L. Johnson (First published by ItWorld.com 2001-03-15)

There isn’t really a whole lot involved when building a module, just a few basic steps and a rule about naming your module. For this example we will building a module named Cool.pm which will contain one function called cool().

The basic outline of our module is as follows:

    package Cool;         # your package/module name

    require Exporter;     # setting to use Exporter's import()
    @ISA = qw(Exporter);  # routine

    @EXPORT = qw();       # Defining how and what to export
    @EXPORT_OK = qw(cool);

                          # your code here
    sub cool {
        print "Hey, cool!\n";
    }

    1;                    # end with a true statement
    __END__
                          # your POD documentation

    =head1 NAME

    Cool -- a useless example module

    =head1 SYNOPSIS

        use Cool qw(cool);
        cool();

    =head1 DESCRIPTION

    This example module merely illustrates the basic components
    of a module. If it were a real module we would document it
    properly.

So we have six basics things to do in building a module: 1) declare the package; 2) set up exportation; 3) what to export, and how; 4) our actual code; 5) end with a true statement; 6) include our POD documentation for the module. Pretty simple right? Let’s look at each of the steps in turn.

Step one is declaring our package name. The rule is, our package name will be the same (case sensitive) as the filename we store this in (except that we put a .pm extension on the file). So our module will exist in a file called ‘Cool.pm’. If we build a nested package like Cool::Kewler, we would declare that package name and store it in a file name Kewler.pm under a directory in our @INC path named Cool, so it would be in a file named ‘Cool/Kewler.pm’ (we’ll talk more about where to put module files next week).

Step two is pulling in Perl’s Exporter module via the require() statement, and including it in our @ISA array. What this does is gives us a default import() routine (we inherit it from Exporter) so we do not have to build our own (a subject to deep for the present article).

Step three defines what we want to export and how to export it. The @EXPORT array holds whatever functions and variables we want to automatically export in the calling script. The @EXPORT_OK array holds whatever we want to export by demand. What does that mean? Well, if the calling script starts of like:

    #!/usr/bin/perl -w
    use strict;
    use Cool;

It will only receive the functions specified in the @EXPORT array. If the caller does this:

    #!/usr/bin/perl -w
    use strict;
    use Cool qw(cool);

It is asking to import the cool() function from the @EXPORT_OK array. Our module is setup to use the @EXPORT_OK array because that is generally nicer — that way the caller decides what they want to import and doesn’t have to worry about a whole bunch of functions being imported by default.

Step four defines our actual code — in this case a single function named cool() that merely prints a string.

Step five is very simple, but very important — the last statement evaluated by a module must return a true value as an indicator that all has gone well. Using just a ‘1;’ is the standard way of doing this.

Step six is also very important — if you are going to build a module, you should document it with POD so users of it can find out what it does and how to use it. The perlpod manpage (perldoc perlpod) explains the POD markup language we use for documenting modules and scripts.

OK, if you save the above module in a file named ‘Cool.pm’, you can test it out using the following script (in the same directory):

    #!/usr/bin/perl -w
    use strict;
    use Cool qw(cool);
    cool();
    __END__

And, you may also type ‘perldoc Cool’ to bring up the modules POD (again, in the same directory).

*****

More on Context: Boolean and Operator Contexts

Andrew L. Johnson (First published by ItWorld.com 2001-03-08)

Perl has a special kind of scalar context called boolean context — this context occurs inside of conditional expressions (if and while conditions for example) and applies to the arguments of logical operator. In such a context, an expression is evaluated in scalar context and then a truth value (true or false) is determined from the result of that expression.

So, for example, if we want to exit the program with a usage message if no command line arguments were given:

    die usage("No arguments given") unless @ARGV;

(of course, I am assuming a function named ‘usage’ exists and produces a relevant message). This tests the @ARGV array in a boolean scalar context — and recall that when an array is evaluated in a scalar context it returns its size. So, if @ARGV is empty, the result is 0 which is false and so we die() with an appropriate message. Another example might be to test if an array (or any list) has a certain number of some particular entry — we can use grep() to test a condition:

    my @list = (12, 14, 101,102,103, 104, 4);
    if( (grep{$_ > 100} @list) > 3) {
        print "More than 3 items greater than 100\n";
    }

Normally, grep() returns the list of successful elements, but in scalar context it returns the number of successful elements. The greater than operator places the grep() into scalar context, and that result is compared to 3. Logical operators also provide a scalar boolean context to their operands:

    @ARGV || die "No arguments";

Here the @ARGV array is again evaluated in scalar boolean context, so if it contains anything the OR expression is ignored (logical short circuit evaluation), otherwise the left hand side is false and the die() is then called.

Another, very different, kind of context sometimes causes people problems — that is, operator context. Perl variables are not typed according to what kind of data they hold, but operators are associated with particular types of data (numerical or string operators) and Perl will convert data to the appropriate type for the given operator:

    if(21 <  200) {print "True\n"}  # true
    if(21 lt 200) {print "True\n"}  # false

In the second example above the string operator ‘lt’ (less than) is used so Perl converts 21 to "21" and 200 to "200" and compares them according to ascii collating sequence (asciibetical order). The string "21" is greater than the string "200". Such conversion works the other way around as well:

    if( "14" gt "9"){print "True\n"} # false
    if( 14  >  9)   {print "True\n"} # true

As a final warning, converting a number to a string is simple — the string is just the character representation of the number. However, converting strings to numbers isn’t always as clear. For example, how should the string "xyz" be converted? The rule is: if a string begins with something that looks like a number (ignoring any leading whitespace) then convert to the equivelant number, ignoring any non-numeric characters that might follow the number. Otherwise, the numeric value of a string is 0:

    my $a = 14;   # $a is: 14
    $a .= 5;      # $a is: "145"

    my $b = "Hello World";
    $a .= $b;              # $a is: "145Hello World"
    $a += 5;               # $a is: 150
    $a += $b;              # $a is: 150

Here we first set $a to the number 14, then we concatenated it (a string operation) with 5 (so Perl converts the 14 and the 5 to strings and concatenates them into the string "145"). We set $b to just a string and then concatenate with $a giving us a string in $a that starts with a number. When add 5 to $a Perl converts the string "145Hello World" into the number 145 and then adds 5 to it. Lastly, if we try to add $a with the string in $b, Perl treats $b as a number and it evaluates to 0, so $a is left as 150.

If you run that whole last example with warnings turned on (you do run with the -w switch right?) you’ll notice two warnings about using a string in a numeric context. It is easy to mistakenly use a string comparison operator instead of a numeric one (or vica versa), and using warnings will often tell you when you’ve made such a questionable comparison.

*****

Context is Everything

Andrew L. Johnson (First published by ItWorld.com 2001-03-01)

There are two main contexts that you really must be aware of when writing Perl programs: Scalar context and List context. Things can behave differently when evaluated in one or the other of these contexts.

Probably the most well know example is assigning an array:

    my @list   = @array; # @array is in list context
    my $scalar = @array; # @array is in scalar context

In the first case above, @array is being assigned to another array, providing list context, so it returns the list of its contents. In the second example, @array is being assigned to a scalar variable thus putting it in scalar context — an array in scalar context returns the size of the array. Now, one twist on the second example above is:

    my ($scalar) = @array; # @array is in list context;

Here the parentheses around the scalar mean that the left hand side of the assignment is a list of scalars (in this case, a list of length one), thus the @array is in list context. Now @array returns the list of its contents and the first element is assigned to $scalar (the rest of the list returned by the array is ignored).

Another example is using the keys() function of a hash — in list context it returns the list of keys in the given hash, but in scalar context it returns the number of keys in the hash. A hash itself in list context returns the full list of key value pairs (in the same sequence as the keys() function returns the keys), but in scalar context it does something rather different — it returns information about the underlying hash structure:

    my %hash = (one => 1, two => 2);
    print %hash, "\n";        # prints: one1two2
    print scalar %hash, "\n"; # prints: 2/8

The print() function provides a list context (the function expects to receive a list of arguments). The scalar() function can be used to explicitly evaluate an expression in a scalar context (as we do in the last example above). That final version is telling us that our hash currently has 8 buckets allocated and that two are being used.

Now, we’ve already seen that functions can return different things depending on context with the keys() function. Another example is the localtime() function (see: perldoc -f localtime). Operators can also act context dependently, as with the match operator m// (and that behavior is also modified by the /g modifier). The x operator (string replication) works differently if its left hand side is a scalar or a list.

Functions themselves may also supply context to their arguments. Take the join() function for example — the first thing it expects as an argument is a scalar value holding the string to be used as the join separator. If you thought you could just put all the arguments to join into a single array and pass that you wouldn’t get the results you wanted:

    my @args = (":", 1,2,3);
    my $str = join @args;
    print $str;              # $str is empty

Here the join() function expected a scalar as the first argument an so evaluated the @args array in a scalar context to get the separator character (in this case the size of the array which is 4), and there weren’t any following arguments to join together.

If a function or operator behaves differently depending on scalar or list context it is documented in the relevant documentation pages (see the perlfunc and perlop manpages).

*****

Using Here-docs

Andrew L. Johnson (First published by ItWorld.com 2001-02-22)

Perl has several quoting mechanisms, but the here-doc is the most convenient for quoting multiline strings. You may be familiar with such constructs from shell programming. The basic syntax is to follow doubled left-angle brackets with a terminator string and a semi-colon, all lines after that up to the terminator are quoted.

    my $string = <<EOS;
    this is part of the string
    and so is this
    EOS

    # now we are out of the quoted string

There are a few points to be aware of: By default, such a string is considered to be double quoted (so variable interpolation works as expected); and the final terminator should be flush with the left margin and by itself on a line (no trailing spaces or tabs or anything else). Thus, even though all the code is indented here, you are to assume that it is flush left. To achieve a single version you surround your initial terminator specifier with single quotes:

    my $string = <<'EOS';
        this is a
        single quoted
        here-doc
    EOS

Note, all indentation and spacing is preserved, this is simple a multiline string. Besides single or double quotes, you can also use backtics to have your multiline string executed as shell commands:

    print <<`SHELL`;
    echo Hello
    ls -l
    SHELL

One good reason to use here-docs is to avoid multiple print statements, say when printing several lines of HTML:

    print "<html>\n";
    print "<head><title>Whatever</title></head>\n";
    print "<body>\n";
    # and more of the same

    print <<HTML;
    <html>
    <head><title>Whatever</title></head>
    <body>
    HTML

Now we’ve avoided explicitly using quotes and newlines and our output is contained in a nice little chunk.

Here-docs can also be stacked, either for direct printing (not so useful perhaps) or when providing multiline quoted strings as arguments to some function:

    $two = "this is the second\n";
    print <<FIRST, $two, <<THIRD;
    Here is the
    first string
    FIRST
    And here is
    the third
    THIRD

    foo(<<FIRST, $two, <<THIRD);
    Here is the
    first argument
    FIRST
    And here is
    the third
    THIRD

    sub foo {
        my($first, $second, $third) = @_;
        print "$first$second$third";
    }

The perldata manpage documents here-doc syntax along with a method a allowing for indentation. The faqs (perlfaq4 in particular) also shows several methods of arranging various indentation and block-like formatting of here-docs.

Using File::Find

Andrew L. Johnson (First published by ItWorld.com 2001-02-15)

Reading the contents of a directory is easy using the opendir() and readdir() built-in functions, but once you need to dig a little deeper in the filesystem (recursively searching one or more directories), this method becomes cumbersome and sometimes tricky if your system supports symbolic links.

The File::Find module (which ships with perl) makes traversing directories and acting on the contents very easy and safe. The module exports one function (find()) which can be called in two ways. In the simplest method, you pass it a function reference and a list of directories to search. The function reference will contain the code you want to use to process each filesystem entry found by the find() function. For example, let’s say we want to do some routine cleanup in a directory. I often use LaTeX for my writing and generate either .dvi, .ps, or .pdf files for typeset output. I also often fail to delete these generated files and they end up needlessly using space. Even though hard drive space is cheap these days, I still want to clean out my writing directory from time to time (which contains a large number of subdirectories of various depths).

If I haven’t accessed a generated typeset file (of any variety) for more than say a week, I might as well delete it (I can always regenerate it from the LaTeX sources if I need to print it again later).

    #!/usr/bin/perl -w
    use strict;
    use File::Find;
    my @dirs = @ARGV or die "No valid directory argument(s)";
    find( sub{
              m/\.(dvi|ps|pdf)$/
              and -A > 7
              and print "$File::Find::name\n";
          },
          @dirs,
    );

The first argument to the find function is my subroutine reference (in this case an anonymous subroutine, but we could have used a reference to a real subroutine), and then the list of arguments from the command line. When find() runs it does a chdir() into each directory in turn (and any subdirectories) and sets $_ to be each filename found (the complete pathname of the file is given in the special package variable: $File::Find::name). So we first check if has one of the extensions we are interested in, and that its access time is greater than seven days and then we print the full pathname. In the real program we would use the unlink() function to delete each such file rather than just printing it (but if you’re developing such a script, please test it with print() until you are sure it is finding only what you wanted before you start deleting files with the unlink() function).

The alternate method of calling find() is to pass a hash reference as the first argument, and then the list of directories to search. The hash allows you to specify alternate behaviors for the find() function. There are several keys you may set in this hash, the more common ones are:

    wanted  => your sub reference
    bydepth => depth first searching
    follow  => follow symbolic links (be careful!)
    nochdir => don't chdir() into each directory

A few variables are also available to you within your subroutine, the most useful are:

    $_                    name of file
    $File::Find::dir      directory currently being searched
    $File::Find::name     Full pathname of file

File::Find can be utilized for all sorts of filesystem searching and administrative tasks and is a good tool to add to your repertoire.

*****