Loginskip to content

Archive for November, 2006

Garbage collection in Perl does not happen asychronously,

Thursday, November 2nd, 2006

Garbage collection in Perl does not happen asychronously, but is entirely deterministic (except for the order in which objects are freed during global destruction at the end of a program). When the reference count on an object drops to zero, it is destroyed immediately. Although Java allows you to inherit from only one concrete base class and multiple abstract classes, Perl makes no distinction between the two kinds of class and says that you can do as much multiple inheritance as you like. You may find that your fears about incestuous inheritance relationships do not come to pass, however. Reflection in Perl is accomplished by several methods. If the object $obj is implemented as a hash, you can discover its set of attributes with keys%$obj. You can find out if it supports a given method methwith $obj->can(’meth’), and you can get a list of all the methods it supports by traversing the symbol table for the package looking for subroutines. Finally, you can construct its inheritance hierarchy by following the @ISA array package variable or by using the CPAN module Class::ISA (http://search.cpan.org/search?dist=Class-ISA) which provides a friendly interface for that. Finalizer chaining does not happen automatically in Perl either the Perl term for finalizeis DESTROY and for much the same reasons as in Java. In keeping with the Perl philosophy of permissiveness, there is no equivalent of finalto prevent a method from being overridden in a derived class. 12.5.2 Specific Tips for the C++ Programmer The lack of strong typing which probably seemed liberating to the C programmer probably seems offensive to you. (Perl may seem irritatingly free-form until you grasp the Perl ethos.) Perl does have operator overloading, although it isn’t used as often as you’d think. perldocoverload tells you all about it. Because Perl doesn’t have strong typing, you won’t need to construct template classes for generic operations since they’re generic to begin with. We personally find the use of references in C++ to be annoying, because a change in a function prototype can change the actual value that is passed in a call to the function. If you actually like this feature, you can replicate it after a fashion with what Perl calls subroutine prototypes. They won’t look like prototypes to you they’re more like patterns specifying the order and types of arguments that can be passed but one of the consequences is that you can construct a prototype that will allow a subroutine to be called with an array and receive a reference to that array where it would normally receive the list of all the elements in the array. We suggest you not be too hasty to avail yourself of this feature, primarily because unlike C++, Perl does not require prototypes to be declared in order for a program to work, and if yours somehow fail to get declared or get declared too late then the semantics of your program will change in wonderfully unpredictable ways. Chapter 13. Debugging CGI Programs

Hint: This post is supported by Gama besplatan domen provider

and the rest of the program will be

Thursday, November 2nd, 2006

and the rest of the program will be ignored. (Actually, it becomes available for your program to read through the filehandle DATA if you wish.) Perl’s quoting rules will be a welcome relief no more fighting to determine the combination of ‘, “, and to get what you want. The simple rule is: Interpolation (the Perl term for what you call parameter substitution) is determined only by the type of the outermost quotes. For instance, if you want to pass a variable that may contain spaces to the grepprogram, just say system “grep -s ‘$quote’ @files”; and the double quoting around the whole command ensures that $quote and @filesare interpolated. The former is decorated with single quotes, and the elements of the latter are space-separated by default, exactly what we want. What if you want to embed $quotebetween double quotes instead? Perl allows you to put a double quote in a double-quoted string by preceding it with a backslash, but there is a cooler solution using Perl’s qq quoting operator, which allows you to choose a different delimiter and thereby eliminate the need to backwhack quotation marks: system qq(grep -s “$quote” @files); If $quote can contain absolutely any characters, however, and you don’t want any of them to be interpreted as metacharacters either by the shell or by the grepcommand, you need to make sure that all of the potential troublemakers are escaped, and Perl has a special escape sequence pair just for the job: system “grep -s Q$quoteE @files” Extending this to handle the possibility of members of @files containing white space or metacharacters is left as an exercise for the reader. Speaking of operators, Perl uses eq and nefor strings, and ==and != for numbers, which you probably think is the wrong way around. You’ll be pleased to learn that the return code for the backticks and system command is stored in the familiar $?. However, that return code is not the one you’re used to; it’s the one that C programmers get from their system call, so shift it right by eight bits to get the code you want. (The bottom eight bits contain information most people aren’t interested in, like whether the program core dumped or what signal interrupted it, at least on Unix.) 12.5 Tips for the C++ or Java Programmer The object-oriented features of Perl may appear frighteningly sparse; in fact, they are the consequence of a careful design to avoid unnecessarily bloating the language. For instance, there is no thiskeyword for referring to the current object in a method; instead, it’s passed as the first parameter, and you can pick your own name. Most people pick $selfbecause the declaration my $self has a nice ring to it (or because they are former Smalltalk programmers).
Note: If you are looking for good and high quality web space to host and run your java application check Vision java hosting services

and the rest of the program will be

Thursday, November 2nd, 2006

Run-time semantics alone determine whether a routine is an object method or a class method, so you can easily write one that swings both ways. (A common idiom makes an object method out of a constructor.) There is nothing special about a constructor method in Perl; you can call it anything you want, but most Perl programmers call it new to make you feel welcome. Not only is the underlying implementation of an object exposed, but you get to implement it yourself from a bewildering array of choices. (The one you’ll feel most comfortable with is a hash; don’t even look at the others until you’ve gotten the hash under your belt.) And there is no native support for creating instance data attributes or inheriting them, although there are optional pragmata (base, fields) that provide some syntactical sugaring for doing so. Regardless, your callers should not access your instance data directly but instead via accessor methods. You may use a module (such as Class::Struct) to create those methods automatically, although it seems that any class worth writing these days requires custom logic in each accessor method anyway. It is possible for a caller to modify the object’s instance data directly unless you go to great lengths to make the instance data as private as you’re accustomed to. (See “Closures as Objects” in the perltoot documentation page.) This is rarely perceived as a problem in Perl since the philosophy is that people who fiddle with instance data directly when the specification for the public interface to the class doesn’t permit it deserves whatever they get. Because there are no function prototypes (of the kind you’re used to) in Perl, there is no method overloading with different prototype signatures. You get to validate the number and type of arguments you’re called with at run time, if you want. You cannot enforce abstract (pure virtual) methods to be overridden at compile time, but you can create one that throws a run-time exception, unless it has been overridden. You’ll find more methodical comparisons among Perl, C++, and Java in Damian Conway’s excellent book, Object-Oriented Perl (Manning, 1999). 12.5.1 Specific Tips for the Java Programmer In Perl you have the option of creating a program that is not at all object-oriented and horrors isn’t even a class. This is actually the best approach for solving many problems, as hard as it may be to get out of the habit of doing a class hierarchy decomposition every time you write a program. The issue of “to what extent code you publish for reuse should be object-oriented” has engendered fiercely partisan arguments in the Perl community, with impassioned advocates at both ends of the spectrum. The upshot of this is that you can take whatever position you want and justify it by an appeal to authority.[4] Our rule of thumb is: If you’re writing something just for your own use which is not likely to have significant reusability, the overhead for making it object-oriented is unlikely to be worthwhile. If you’re writing something to be used by other people, an O-O interface will probably be worth the effort, considering how neatly it separates the interface from the implementation and relieves you of the need to provide any more support than simply ensuring that the interface lives up to its promises. [4] Use everything to your advantage …
Note: If you are looking for good and high quality web space to host and run your java application check Vision java hosting services

do so with a reference to a hash

Thursday, November 2nd, 2006

#!/usr/local/bin/perl use myconsts qw(PI E); print “PI*E = “, PI * E, “n”; % mytest PI*E = 8.53973421775655 That’s worth a Perl of Wisdom: Put common sets of constants in their own modules using the Exporter, and use the modules where needed. If you prefer to use EQUIVALENCE well, it’s time to break the habit. You can create aliases for the same variable in Perl, but it’s rarely useful. The Aliasmodule provides an easy way of doing this if you must. If you want to alias one variable to the storage space of many, or vice-versa, as you can with EQUIVALENCE, well, you can’t. And you’ll get no sympathy from us. If you are a FORTRAN programmer who never uses, nor intends to use, EQUIVALENCE statements or COMMONblocks, good! God only knows the measure of misery and heartache that has been inflicted on humanity by these monstrosities. 12.4 Tips for the Shell Programmer You and the C programmer are Perl’s favorite children (assuming that by “shell” we are talking about the Unix variety; the DOS/NT command line interface is quite a different beast). You alone among the other newcomers think it’s quite natural for scalar variables to start with $, and you just need to get used to the fact that the $ is there during assignment as well as during reference. It may have seemed stifling at first that calls to external programs had to be wrapped in system(), although you were right at home with the use of backticks (“) to capture program output. But if you tried to compensate by using Shell.pm just to avoid those system() calls, try doing without. A shell script needs a low syntactical overhead for calling external programs because it can do so little itself; Perl can do so much that calls to other programs are much less frequent. Overcome the tendency to call those beloved programs like cut, head, sed, and yes, even awk for text filtering; once you’ve learned to do the same things in Perl, you will seldom feel the need to go back. If you try converting a shell script to Perl, you’ll find this principle rearing its head as you repeatedly excise calls to utility programs. A shell program is interpreted a line at a time, which means that you can have any old junk representing your thoughts to date on the future direction of your script, as long as it won’t actually get as far as executing any of those statements. Perl requires that the whole script be syntactically correct before it tries executing any of it, which means that you can’t even hide something in an if(0){ }block without making sure that it is syntactically correct. But you can achieve the effect you want by inserting a line consisting solely of __END__

Hint: If you are looking for very good and affordable webspace to host and run your j2ee hosting application check Virtualwebstudio j2ee web hosting services

do so with a reference to a hash

Thursday, November 2nd, 2006

do so with a reference to a hash (or an object); but in practice, most of the uses you’d have for those things aren’t necessary. There’s no need to write special code for linked lists (although there’s a book that shows you how if you have some exotic need[3]); Perl’s arrays grow on demand quite well enough. Most of the reasons to construct trees are taken care of by Perl’s efficient algorithm for looking up elements in hashes. (Again, if you have a need that somehow manages to overwhelm this mechanism, the same book provides alternatives.) [3] Mastering Algorithms with Perl, by Jon Orwant, Jarkko Hietaniemi and John Macdonald (O’Reilly, 1999). Some of the system calls you’re accustomed to look like they were taken behind the woodshed by the tool set and given a workover to extend their usefulness. Instead of taking just one file as argument, chmod, chown, and unlinkall take a list of filenames as arguments instead of just one. Oddly enough, mkdirand rmdirdo not take lists of directory names even though their command line counterparts do. ‘lint’ is spelled ‘usestrict’ and ‘-w’ in Perl. 12.3 Tips for the FORTRAN Programmer Perl code appears quite odd to a FORTRAN programer. FORTRAN uses ( ) (parentheses) exclusively for indices. An array element has the form ARRELM(1), a matrix element has the form MATELM(1,1), and a subroutine call takes the form CALL ASUB ( VALUE1, VALUE2 ), whereas Perl uses ( ) (parentheses), [ ] (brackets), and { } (braces) in various ways depending on data type and function. For instance, whereas $a[$i]refers to an element of an array, $a{$i} refers to a value in a hash. Forgetting the proper uses of these separators is a common error for the neophyte Perler experienced in FORTRAN. If you use FORTRAN for its native handling of high-precision floating point numbers (and if you don’t what are you using it for?), then you’ll find Perl’s IEEE floating point too imprecise (just as if you were to program in C). You may find solace in the PDLmodule from CPAN or the Math::BigFloatmodule in the Perl core. COMMON blocks are an evil way of passing around scads of global variables at the same time and are used far more often in FORTRAN than they should be. (If you want to define common constants used by many FORTRAN files, use a preprocessor; this isn’t the ’60s, you know.) The way to make available a set of constants that share some common theme in Perl is to put them into a module, using the Exporter, and then import them where you want them: % cat myconsts.pm package myconsts; use Exporter; @ISA = qw(Exporter); @EXPORT_OK = qw(PI E); use constant PI => 3.14159265; use constant E => 2.71828183; 1; # Modules must return a true value % cat mytest

Hint: If you are looking for very good and affordable webspace to host and run your j2ee hosting application check Virtualwebstudio j2ee web hosting services

You need to make certain adjustments when coming

Thursday, November 2nd, 2006

You need to make certain adjustments when coming to Perl from more familiar languages: Variables begin with type-specifying punctuation characters[1] referred to by some as sigils. It’s not quite the revenge of Hungarian notation no mblock *apmblkMessages[]here, thank goodness. The hardest thing for a Perl beginner to assimilate is that while, for example, @animals is the name of an array, $animals[14] is the name of a particular element in it, and @animals[2..7]is the name of a slice of that array. Just remember the rule that when you’re referring to exactly one thing, it’s a scalar and therefore begins with a $, whereas multiple things are an array sort of and begin with an @ sign. [1] Well, most of them. Filehandles don’t, unless you create one in a scalar with IO::FileHandle or the new 5.6 feature or assign a typeglob alias. And you’d have a hard time telling the difference between an lvaluable subroutine and a variable. Function arguments don’t have to be surrounded by parentheses. Putting them in won’t hurt, but knowing when you can leave them out makes your program easier to read. (See “The Case of the Vanishing Parentheses” in Chapter 4.) Because Perl insists on conditional clauses being blocks, there is no “dangling else” problem. 12.2 Tips for the C Programmer You no longer must declare all your variables at the beginning of a block before any executable code. In fact, declaring them as late as possible is good practice because it keeps statements that belong to the same functional element together. Perl’s scalar variables are not strongly typed. They can switch oyster-like between integers, floating point numbers, and strings according to your whim (although if you ever turn a reference into a string, you can’t turn it back). Even when you assign an object of a specific class to one, nothing stops you from overwriting it with an object of another class or something that isn’t even an object at all. Polymorphism at its best. There is (as of version 5.005) the capability to declare to the compiler that a scalar is an object belonging to a particular class (myDog$spot), but even that is only a loose agreement that you can break any time you feel like it.[2] (It allows for compile-time checking of accesses to fields, which are one way of implementing instance data.) [2] As of press time, Damian Conway had mooted a module which could restrict a variable to a particular object class, but not even the name had been agreed on. Dynamic (i.e., nonlexical) variables have global scope unless qualified with a local statement. But you won’t want to program in this BASIC style anyway; declare all variables as lexical, and the scoping rules are the same ones you’re already used to. Your days of wondering whether your frees match your mallocs are over! It is very hard to create a memory leak accidentally in Perl. Whenever you see code like this $soldier = make_warrior (’squaddie’); sub make_warrior { my $fighter;

Hint: If you are looking for very good and affordable webspace to host and run your j2ee hosting application check Virtualwebstudio j2ee web hosting services

You need to make certain adjustments when coming

Thursday, November 2nd, 2006

… return $fighter; } which would make your heart leap into your mouth in C, relax. In C terms, $fighteris being allocated from the heap, not from the stack, so it doesn’t become vulnerable as soon as the stack frame for make_warrior is unwound. Furthermore, the space allocated to $fighter doesn’t get freed as long as there is a reference to it, and $soldieris such a reference. As long as something needs the space, it remains allocated, and as soon as nothing needs it, it gets freed. This is what you’re used to spending all that time agonizing about in C. Functions don’t need ()after them if you have no arguments to supply them, but if you do this with functions of your own, then their definition (or a prototype declaration) should precede their use. Although in C the expressions a[i]and i[a] are equivalent, the corresponding expressions in Perl are not. If you find this surprising, you may need more help than we can provide. While Perl inherits many operators (and their precedence) from C and probably strives hardest to be accessible to the C programmer, it also strives to be as orthogonal as possible. For example, while Perl provides the same &&and || operators as C, they return their inputs instead of a Boolean value. In other words, $a||$bis $aif $a is true; otherwise it is $b. (The operators short-circuit the same way). In another example, Perl figures anything that could be an lvalue, should be, so a term containing the trinary (?:) operator is an lvalue if its left and right parts are lvalues. The substrfunction is an lvalue if the result of its first argument is modifiable. And as of version 5.6.0, you can even (in an experimental feature) create a function of your own that is an lvalue. Although no switch statement exists in Perl, this hasn’t stopped people from inventing several dozen ways of simulating one, the latest being Switch.pm, a module presented by Damian Conway at the fourth annual Perl Conference (http://conference.perl.com/). The reason there isn’t a switch is that when you start implementing one, your average switch statement seems very limited when you consider the breadth of Perl’s operators. Why should each case be just a simple test for equality? Why not be able to compare against a regular expression as well? (Even the Bourne shell manages that.) Why shut out the vast set of logical comparisons? Until Switch.pm, it appeared that nothing could be implemented that wouldn’t look like a poor cousin to the rest of the language. (It now seems that some version of Switch.pm will be incorporated into the core Perl 6 language.) Hierarchical data structures are thoroughly typeless (atypical?) in Perl; no more fiddling with union statements to traverse heterogeneous lists. Sometimes, people are afraid this means that they must pedantically check the type of every element of a complex object themselves to make sure they don’t dereference something incorrectly; that is, until they realize that they were probably the ones who constructed the object in the first place, and they can usually trust themselves. Don’t look for a counterpart to struct expecting that you’ll use it nearly as much. If you want to achieve exactly the same effect (named members of a compound variable) you can

Hint: If you are looking for very good and affordable webspace to host and run your j2ee hosting application check Virtualwebstudio j2ee web hosting services

Avoid unnecessary copying. Perl loves to copy data

Thursday, November 2nd, 2006

If one part of your program writes a temporary file for another part to read, you can change this to use a pipe instead, but only if you can get those two parts of your program to execute reasonably concurrently. Otherwise you’ll fill up the pipe when you write to it, and your program will twiddle its virtual thumbs waiting for the pipe to be unclogged. (If you’ve ever needed a plumber on the weekend, you know the feeling.) If you’ve coded something like this, consider breaking it into two separate programs that can be run concurrently and piped together. Chapter 12. Perl as a Second Language “Through the darkness of future past, the magician longs to see. One chants out between two worlds, fire walk with me!” BOB in Twin Peaks Many people come to Perl after already learning one or more other computer languages. (It is debatable whether Perl is a good choice as a first language for a new computer programmer. In our opinion, it is not, and one should first learn a procedural language such as C and an object-oriented language such as Java if for no other reason than to experience that great relief that comes when you start programming in Perl.) For those of you whose strength lies in one of the languages listed in this chapter, here are tips specifically for you. There is already much good information in this respect in the perltrapmanual page; we have not duplicated it here, but urge you to look it up. You will also find Perl: The Programmer’s Companion, by Nigel Chapman (Wiley, 1998), to be a good introduction to Perl. 12.1 Tips for Everyman

Hint: This post is supported by Gama web hosting hrvatska services

Avoid unnecessary copying. Perl loves to copy data

Thursday, November 2nd, 2006

Avoid unnecessary copying. Perl loves to copy data around. When you write a subroutine, say sub bake { my ($temperature, $how_long) = @_; […] it’s convenient to think of its first line as a kind of prototype, but in fact it’s copying data. You may need to rethink this approach when passing huge variables to a subroutine. Caveat: While it’s fine to get your data directly from @_, be aware that @_ is an alias for your original arguments; be sure you don’t modify any of them without documenting the action. Furthermore, named parameter passing works by copying @_ into a hash; you’ll have to come up with another way to recognize nonpositional arguments. Sean M. Burke points out another application of this principle: Once I wrote a program to do some pretty horrific data conversion on an idiosyncratic markup language. Now, line-at-a-time processing was right out the notation was SGML-like, i.e., freeform with its white space. Most or all of the manipulation was of the form $lexicon =~ s{(.*?)} {handle_subhead($1)}ieg; The problem was that $lexicon was quite large (250+ KB), and applying lots and lots and LOTS of such regex search and replace operations took forever! Why? Because all the modifications of a large scalar involved copying it each time. One day it occurred to me that no replacement operation involved crossing entry boundaries (i.e., record boundaries), so I changed the program to chop the lexicon into entries, and then applied the string replacements to each of those via a foreach. Since each entry was only ~2KB, there was MUCH less painful swapping, so the program ran about 50 times faster! 11.3.3 Improving Disk Space Usage Avoid temporary files. If your operating system supports pipes, use them. Even if your operating system doesn’t support them, Perl on your system might; try it.[5] [5] As of version 5.6.0, fork in Perl works on Windows.

Hint: This post is supported by Gama web hosting hrvatska services

print “Array : “, procsize() - $base; First

Thursday, November 2nd, 2006

for my $line () { … } because that would read all the lines of the file into a list and then iterate through them one at a time. If you look at that example with scorn, thinking, “But I would never do that,” then how about this? for my $file (grep !/^..?$/, readdir DIR) { … } Looks like an innocent enough standard idiom, and indeed it’s quite safe, as long as you don’t have any directories containing a humongous number of entries. But if you do, be aware that it requires temporary storage for a list nearly twice the size of those entries. If you’d like to bulletproof the code against that contingency, try this: while (defined (my $file = readdir DIR)) { next if $file =~ /^..?$/; … } If you have very large hashes, consider tieing them to a database to trade memory for speed and disk space: use DB_File; # There are several alternatives to this my %large_hash; tie %large_hash, # The hash to tie ‘DB_File’, # The type of database to use ‘database’; # The file to store the database in # use %large_hash… untie %large_hash; # Close the database file However, you could easily abnegate your savings by using a common idiom: for my $key (keys %large_hash) { … } # wrong! because this will cause Perl to form in memory the list of all the keys. Use eachinstead, because it returns only two things at a time (or one, in scalar context): while (my ($key, $value) = each %large_hash) { … } (Although you can’t add or delete hash elements inside the loop.) If your hash values contain references (your hash keys can’t, unless you’re using the Tie::RefHashmodule), then you should use Gurusamy Sarathy’s MLDBM (MultiLevel Database) module from CPAN (http://search.cpan.org/search?dist=MLDBM). Otherwise, the referents to which your references refer will not get stored in the database.
Note: If you are looking for cheap and inexpensive provider to host and run your tomcat application check Actions tomcat hosting services