dcsimg

The Perl You Need to Know, Part II: Working with Nested Subroutines Page 7

By Stas Bekman (Send Email)
Posted Sep 1, 2000


The approach given above is generally not recommended because most Perl programmers will not expect to be changed by the function; the example where we used $counter, i.e. pass-by-reference would be preferred.

Here is a solution that avoids the problem entirely by splitting the code into two files; the first is really just a wrapper and loader, the second file contains the heart of the code.

  multirun6.pl
  -----------
  #!/usr/bin/perl -w
  
  use strict;
  require 'multirun6-lib.pl' ;
  
  for (1..3){
    print "run: [time sh]\n";
    run();
  }

Separate file:

  multirun6-lib.pl
  ----------------
  use strict ;
  
  my ;
  sub run {
     = 0;
    increment_counter();
    increment_counter();
  }
  
  sub increment_counter{
    ++;
    print "Counter is equal to  !\n";
  }
  
  1 ;

Now you have at least six workarounds to choose from.

For more information please refer to perlref and perlsub manpages.

perldoc's Rarely Known But Very Useful Options

It's a known fact, that one cannot become a Perl hacker and especially mod_perl hacker without knowing how to read Perl documentation and search through it. Books are good, but an easily accessible and searchable Perl reference at your fingertips is a great time saver. It always has the up-to-date information for the version of perl you're using.

Of course you can use online Perl documentation at the Web. I prefer http://theoryx5.uwinnipeg.ca/CPAN/perl/ to the official URL: http://www.perl.com/pub/v/documentation is very slow :( . The perldoc utility provides you with access to the documentation installed on your system. To find out what Perl manpages are available execute:

  % perldoc perl

To find what functions perl has, execute:

  % perldoc perlfunc

To learn the syntax and to find examples of a specific function, you would execute (e.g. for open()):

  % perldoc -f open

Note: In perl5.005_03 and earlier, there is a bug in this and the -q options of perldoc. It won't call pod2man, but will display the section in POD format instead. Despite this bug it's still readable and very useful.

The Perl FAQ (perlfaq manpage) is in several sections. To search through the sections for open you would execute:

  % perldoc -q open

This will show you all the matching Question and Answer sections, still in POD format.

To read the perldoc manpage you would execute:

  % perldoc perldoc

References

  • Online documentation: http://theoryx5.uwinnipeg.ca/CPAN/perl/ http://www.perl.com/pub/v/documentation/

  • The book Programming Perl 3rd edition by L. Wall, T. Christiansen and J. Orwant (also known as the "Camel" book, named after the camel picture on the cover of the book). You want to refer to Chapter 8 that talks about nested subroutines among other things.

  • The perlref and perlsub man pages.

Want to discuss Perl and mod_perl with other Apache Today readers? Then check out the perl discussions at Apache Today Discussions.



Comment and Contribute

Your name/nickname

Your email

(Maximum characters: 1200). You have characters left.