Flylib.com

Books Software

 
 
 

Command-Line Options

B.17 Command-Line Options

There are many different command-line options available in Perl; many let you write useful programs directly from the command line. See the perlrun manpage .

 

B.18 Built in Variables

Perl has dozens of built-in variables (like @ARGV and $0 ), which provide useful information or control the operation of Perl itself. See the perlvar manpage .

 

B.19 Syntax Extensions

There are more tricks you could do with Perl syntax, including the continue block and the BEGIN block. See the perlsyn and perlmod manpages.

 

B.20 References

Perl's references are similar to C's pointers, but in operation, they're more like what you have in Pascal or Ada. A reference "points" to a memory location, but because there's no pointer arithmetic or direct memory allocation and deallocation, you can be sure that any reference you have is a valid one. References allow object-oriented programming and complex data structures, among other nifty tricks. See the perlreftut and perlref manpages.

B.20.1 Complex Data Structures

References allow us to make complex data structures in Perl. For example, suppose you want a two-dimensional array? You can do that, [B] or you can do something much more interesting, like have an array of hashes, a hash of hashes, or a hash of arrays of hashes. [B] See the perldsc (data-structures cookbook) and perllol (lists of lists) manpages.

[B] Well, not really, but you can fake it so well that you'll hardly remember that there's a difference.

[B] Actually, you can't make any of these things; these are just verbal shorthands for what's really happening. What we call "an array of arrays" in Perl is really an array of references to arrays.

B.20.2 Object-Oriented Programming

Yes, Perl has objects; it's buzzword -compatible with all of those other languages. Object-oriented (OO) programming lets you create your own user -defined datatypes with associated abilities , using inheritance, overriding, and dynamic method lookup. [B]

[B] OO has its own set of jargon words. In fact, the terms used in any one OO language aren't even the same ones that are typically used in another.

Unlike some object-oriented languages, though, Perl doesn't force you to use objects. (Even many object-oriented modules can be used without understanding objects.) But if your program is going to be larger than N lines of code, it may be more efficient for the programmer (if a tiny bit slower at runtime) to make it object-oriented. No one knows the precise value of N, but we estimate it's around a few thousand or so. See the perlobj and perlboot manpages for a start, and Damian Conway's excellent Object-Oriented Perl (Manning Press) for more advanced information.

B.20.3 Anonymous Subroutines and Closures

Odd as it may sound at first, it can be useful to have a subroutine without a name . Such subroutines can be passed as parameters to other subroutines, or they can be accessed via arrays or hashes to make jump tables.

Closures are a powerful concept that comes to Perl from the world of Lisp. A closure is ( roughly speaking) an anonymous subroutine with its own private data.

 

B.21 Tied Variables

Do you remember how the DBM hash (in Chapter 16 ) is "magically" connected to a file, so that accesses to the hash are really working with the corresponding DBM file? You can actually make any variable magical in that way. A tied variable may be accessed like any other, but using your own code behind the scenes. So you could make a scalar that is really stored on a remote machine, or an array that always stays sorted. See the perltie manpage .