|
Softpanorama |
May the source be with you, but remember the KISS principle ;-)
|
|
Old News |
Recommended Links | Recommended Articles | Perl programming environment | Debugging | Perl for Win32 | ||
| Sort | Tips | Beautifiers | Humor | Etc |
See Skeptical introduction to Perl for the introduction.
|
In the Beginning, some time around 1960, every part of your program had access to all the variables in every other part of the program. That turned out to be a problem, so language designers invented local variables, which were visible in only a small part of the program. That way, programmers who used a variable x could be sure that nobody was able to tamper with the contents of x behind their back. They could also be sure that by using x they weren't tampering with someone else's variable by mistake.
Every programming language has a philosophy, and these days most of these philosophies have to do with the way the names of variables are managed. Details of which variables are visible to which parts of the program, and what names mean what, and when, are of prime importance. The details vary from somewhat baroque, in languages like Lisp, to extremely baroque, in languages like C++. Perl unfortunately, falls somewhere towards the rococo end of this scale.
The problem with Perl isn't that it has no clearly-defined system of name management, but rather that it two systems, both working at once. Here's the Big Secret about Perl variables that most people learn too late: Perl has two completely separate, independent sets of variables. One is left over from Perl 4, and the other is new. The two sets of variables are called `package variables' and `lexical variables', and they have nothing to do with each other.
Package variables came first, so we'll talk about them first. Then we'll see some problems with package variables, and how lexical variables were introduced in Perl 5 to avoid these problems. Finally, we'll see how to get Perl to automatically diagnose places where you might not be getting the variable you meant to get, which can find mistakes before they turn into bugs.
© Copyright 1998 The Perl Journal. Reprinted with permission.
One of the most important new features in Perl 5 was the capability to manage complicated data structures like multidimensional arrays and nested hashes. To enable these, Perl 5 introduced a feature called `references', and using references is the key to managing complicated, structured data in Perl. Unfortunately, there's a lot of funny syntax to learn, and the manual is not as clear in this area as it usually is. The manual is quite complete, and a lot of people find that a problem, because it can be hard to tell what is important and what isn't.
Fortunately, you only need to know 10% of what's in the manual to get 90% of the benefit. This article is going to show you that 10%.
The B::Xref module is used to generate a cross reference listing of all definitions and uses of variables, subroutines and formats in a Perl program. It is implemented as a backend for the Perl compiler.
The report generated is in the following format:
File filename1 Subroutine subname1 Package package1 object1 line numbers object2 line numbers ... Package package2 ...Each File section reports on a single file. Each Subroutine section reports on a single subroutine apart from the special cases "(definitions)" and "(main)". These report, respectively, on subroutine definitions found by the initial symbol table walk and on the main part of the program or module external to all subroutines.
The report is then grouped by the Package of each variable, subroutine or format with the special case "(lexicals)" meaning lexical variables. Each object name (implicitly qualified by its containing Package) includes its type character(s) at the beginning where possible. Lexical variables are easier to track and even included dereferencing information where possible.
The
line numbersare a comma separated list of line numbers (some preceded by code letters) where that object is used in some way. Simple uses aren't preceded by a code letter. Introductions (such as where a lexical is first defined withmy) are indicated with the letter "i". Subroutine and method calls are indicated by the character "&". Subroutine definitions are indicated by "s" and format definitions by "f".Option words are separated by commas (not whitespace) and follow the usual conventions of compiler backend options.
-oFILENAME- Directs output to
FILENAMEinstead of standard output.-r- Raw output. Instead of producing a human-readable report, outputs a line in machine-readable form for each definition/use of a variable/sub/format.
-d- Don't output the "(definitions)" sections.
-D[tO]- (Internal) debug options, probably only useful if
-rincluded. Thetoption prints the objl&
This plugin causes write attempts to fail if the contents of the buffer produce an error when run through "perl -c"
The plugin uses can use autocommands for the BufWriteCmd and FileWriteCmd events, but by default only provides a :Write command to check-then-write.install details Put this file in one of the locations described in :help ftplugin such as "~/.vim/after/ftplugin"; if you want autocommands, which are quirky, define perl_synwrite_au = 1
This is compiler script that runs perl -Wc and parses all error and warnings. For more information how to use compilers in VIM read help.
This version has workaround with redirecting stderr on windows platform so it can run either on unixes or windows.
Enjoy. Lukasinstall details Just put in the $VIMRUNTIME/compiler directory and run :make.
It is a great project to implement versions of standard Unix tools in pure Perl, so that they can run anywhere Perl does. Those utilities might be perfect for those who like me I prefer Unix/Linux as the development platforms, but Windows for my desktop. They also might be an alternative to the Cygwin stuff even for those who do not use Perl for own scripting. They should be the alternative for those who does use Perl. Once Perl has been installed the PPT stuff only needs to be copied somewhere and that directory added to the PATH. You can also learn some great Perl coding tricks by reading the PPT utilities. Thanks for the anonymous feedback to the Softpanorama for this info.
Perl has a large number of command-line options that can help to make your programs more concise and open up many new possibilities for one-off command-line scripts using Perl. In this article we'll look at some of the most useful of these.
Kwiki
The Kwiki motto is a "A Quickie Wiki that's not Tricky." Installing it is pretty straightforward for a site you admin: just install the Perl package (from CPAN or elsewhere), and then type
kwiki-installin a CGI-served directory to create an instance. Installing Kwiki on a server you are not an admin of is more complicated but doable.I found the Kwiki markup not powerful. Some things are impossible with it, such as hyperlinking an arbitrary piece of text to an email address (mail fooish). I also could not find how to link a Wiki page with a text link different from the Wiki page name (like this link to LinkedWikiWikiPage). There is also no support for attachments, HTML markup as an alternative to the Wiki markup, etc. It is disappointing.
Kwiki can use either RCS or Subversion for version control. (Those who wish to use Subversion should check out the corrected Kwiki version as the CPAN Kwiki does not work with the up-to-date Subversion.) Kwiki is easily customizable and has several Kwiki enhancements available. Generally, however, they are less powerful than TWiki's.
All in all, Kwiki is easy to install and customize, but its formatting rules are lacking.
... ... ...
UseModWiki
UseModWiki is a Wiki engine written in Perl. Anecdotally, Wikipedia used this first before re-implementing their current engine. Other sites also use UseModWiki.
UseModWiki is very simple to set up and upgrade. It has a rich syntax, and allows for arbitrary characters in page names. It also supports using some HTML tags instead of the WikiWiki markup. It has other nice features, including search, a list of recent changes, and page history.
For simple Wikis, UseModWiki is a very good choice. I recommend choosing between it and PmWiki based on the feature list of both Wikis.
Several books are available online: see Perlbooks.
Book chapters
Perl Cookbook, 2nd Edition Chapter 1: Strings (PDF)
Sriram Srinivasan. Advanced Perl Programming Chapter 1: Data References and Anonymous Storage
Learning Perl Objects, References & Modules Chapter 3: Introduction to References (PDF)
Learning Perl, 3rd Edition Chapter 4: Subroutines
Mastering Algorithms with Perl Chapter 10: Geometric Algorithms (PDF)
Programming Perl, 2nd Edition Chapter 1: An Overview of Perl
Programming Perl, 3rd Edition Chapter 18: Compiling
Mastering Regular Expressions 1st edition Chapter 4: The Mechanics of Expression Processing
Mastering Regular Expressions, 2nd Edition Chapter 8: Java (PDF) Chapter 9: .NET (PDF)
Perl Debugger Pocket Reference Sample Excerpt: Before You Debug, A Debugger Tutorial, and More (PDF)
Perl for System Administration Chapter 9: Log Files
John Callender. Perl for Web Site Management y Chapter 8: Parsing Web Access Logs
Lincoln Stein, Doug MacEachern Writing Apache Modules with Perl and C Chapter 4: Content Handlers
Actually there are few good online tutorials on Perl.
Practical Perl Programming by A. D. Marshall 1999
Picking Up Perl, a Freely Redistributable Perl Tutorial Book. a small tutorial currently it is in version 0.11.
Perl by Example -- online book by David Medinets More like a reference then tutorial. Still it's one of the few published books available online.
Please note that all O'Reilly Perl books have one chapter online. Those chapters are a nice complement to full online books.
**** http://www.oreilly.com/catalog/perlsysadm/chapter/ch09.html Chapter 9 of "Perl for System Administration" online. Chapter 9 is an in-depth look at one of the more common system administrator's duties: sifting through log files.
The chapter covers everything from basic syslog, text only log files to Microsoft NT, binary log files and how to interpret them using Perl. David N. Blank-Edelman does more than just explain how to grok the files, he addresses several other problems, such as log file rotation and stateful vs stateless data. There is also a very detailed section on log file analysis. He covers several different algorithms for analyzing the logs and turning them into useful data. Also, he addresses the use of databases in the logfile analysis process.
Reviews
As for WEB tutorial IMHO only explanation of a particular language features can probably be useful.
Regular expressions (see special section devoted to this important feature below)
References
General
See also modules, regular expressions
Copyright © 1996-2008 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.
Standard disclaimer: The statements, views and opinions presented on this web page are those of the author and are not endorsed by, nor do they necessarily reflect, the opinions of the author present and former employers, SDNP or any other organization the author may be associated with. We do not warrant the correctness of the information provided or its fitness for any purpose.
Last updated: June 02, 2008