Loginskip to content

Archive for October, 2006

line 2) (Do you need to predeclare print?)

Sunday, October 29th, 2006

line 2) (Do you need to predeclare print?) syntax error at typo.pl line 3, near “print “$message ” Global symbol “message” requires explicit package name at typo.pl line 3. Backslash found where operator expected at typo.pl line 3, near “$message ” (Missing operator before ?) Bareword “n” not allowed while “strict subs” in use at typo.pl line 3. String found where operator expected at typo.pl line 3, at end of line (Missing semicolon on previous line?) Can’t find string terminator ‘”‘ anywhere before EOF at typo.pl line 3. Seven error messages from one typo! Let’s concentrate on the first error and ignore the rest for now. The first error complains about a scalar on line 3 instead of a typo. Luckily, we get a hint about the error: Might be a runaway multi-line “” string starting on line 2 This suggests paying special attention to the use of quote marks on line 2, where we find the string delimited by a double quote (”) and a single quote (’). String delimiters must match at the beginning and end. Since there are no interpolated variables or digraphs in this string, either kind of quote will work, as long as we use the same one at each end of the string. Handle only the first warning or error message output by Perl; don’t bother reading the others, just recompile. Error messages after the first one may be a cascade effect and therefore may be eliminated by removing the cause of the first message. It’s usually not worth the time to analyze each message to determine whether this is the case. 8.1.5 Transposition Regular expressions provide a fertile breeding ground for typos. Suppose you want to match and save the last string of letters preceded by white space on a line: /s([a-z])+$/i Looks right, eh? Try it on an example, though: while () { print “Match = $1n” if /s([a-z])+$/i; } __END__ The boy stood on the burning duck
Note: If you are looking for top 10 and very good webhost to host and run your jsp application check Actions jsp hosting services

line 2) (Do you need to predeclare print?)

Sunday, October 29th, 2006

What comes out but Match = k Oops. We wanted the last word, not the last letter. Here’s an approach to debugging this problem: We printed $1, which is the text saved between parentheses. We can ignore whatever else the regex contains and focus on what’s between the parens, i.e., [a-z], which we immediately recognize as a character class that hasn’t been qualified with a quantifier. Therefore it can represent only one character, which is what we got. (When saving parentheses match more than once in a regex because of a quantifier, only the last match gets saved. One might wish for a switch that would return all the matches in a list.) As with most typos, identifying it is 90% of the goal. The fix is easy to predict: /s([az]+)$/ i. Here’s a higher-level sort of transposition. Can you tell what it is before reading the explanation? #!/usr/bin/perl -w use strict; my $fmt = “%10s “x5; printf “$fmtn”, qw(Kelvin Celsius Rankine Fahrenheit Reaumur); $fmt = “%10.2f “x5; for (my $kelvin = 0; $kelvin += 10; $kelvin < 500) { my $celsius = $kelvin - 273.15; my $rankine = $kelvin * 9 / 5; my $fahrenheit = $rankine - 459.67; my $reaumur = $celsius * 4 / 5; printf "$fmtn",$kelvin,$celsius,$rankine,$fahrenheit, $reaumur; } Run this, and it does not terminate! You will be calculating temperatures beyond those experienced in the Big Bang. But the very first line of output is Useless use of numeric lt (<) in void context at temp.pl line 13. Weird. Line 13 is the printf statement, and it has no <. According to Perl of Wisdom #24, we should start scanning backwards until we find one. There it is at the beginning of the for statement: forgivably, we transposed the test and iterative clauses. If you'd like to know why Perl fingered line 13 and not line 7 (the for statement), you'll find an explanation in Chapter 10. 8.2 A Menagerie of Typos
Note: If you are looking for top 10 and very good webhost to host and run your jsp application check Actions jsp hosting services

use strict $message = “Hello World”; print “$messagen”;

Sunday, October 29th, 2006

Consider a program with one extraneous character: use strict; my $x = 1; my $y = 2; $x == $y; print “$x,$yn”; Run the program and Perl prints: 1,2. No errors or warnings are given; this is expected, since each program statement is valid Perl. Now consider a program that differs from the previous one by one character: use strict; my $x = 1; my $y = 2; $x = $y; print “$x,$yn”; When run, Perl prints: 2,2. Output from the second program shows the value of $xset to the value of $y, whereas the first performs no such operation. What did the first program do? In line 4, $x=$y was intended, but $x==$y was coded. The former is an assignment to modify $x, the latter a Boolean test that modifies neither $xnor $y a particularly nasty problem as both commands are correct Perl syntax, and both execute without a complaint, with or without use strict. All this is very elementary, of course, until you wonder “What if I use -w?” Run our first program with -w and Perl returns: Useless use of numeric eq in a void context. File ‘typo1.pl’; Line 4. This reveals yet another reason to use -w in every program: with it, Perl can warn us that while our mistake resulted in syntactically valid Perl, it was odd Perl. It can’t raise a red flag stating “An extra = on line 4!” but it can point out that we’ve executed an essentially useless operation. 8.1.4 Alteration Let’s examine another single typo program: use strict; $message = “Hello World’; print “$messagen”; exit; On running, Perl returns the following: Scalar found where operator expected at typo.pl line 3, at end of line (Might be a runaway multi-line “” string starting on
Note: If you are looking for good and high quality web space to host and run your java application check Vision java hosting services

use strict $message = “Hello World”; print “$messagen”;

Sunday, October 29th, 2006

use strict $message = “Hello World”; print “$messagen”; exit; On running, Perl returns: HelloWorld. Surprise! The typo prevents strictness from becoming enabled. The absence of the mykeyword allows the assignment to succeed. (Since it’s global, the fact that the assignment happens in an invisible BEGINblock doesn’t matter.) This is one of the most insidious errors conceivable: a single character mistake that causes no warning and no error yet prevents the very error checking the programmer was trying to enable. 8.1.2 A Puzzle Here’s another example of deletion. Can you figure out what’s wrong? use strict; sub foo { my $sref = shift; foreach my $d (@) { find ($sref, $d); } } sub find { } The result: Global symbol “$d” requires explicit package name at typo.pl line 6. syntax error at typo.pl line 7, near “}” syntax error at typo.pl line 8, near “}” Execution of typo.pl aborted due to compilation errors. foreach my $d (@)should instead be foreach my $d (@_). Well, @) is a valid variable in Perl a strange variable to be sure, and not the kind of thing you want to be using intentionally but a variable nevertheless. This robbed the foreach list of its closing parenthesis and caused a wonderful cascade of nonsense. 8.1.3 Insertion Insertion typos probably occur as often as deletion typos, though with different pathologies. An additional character in a variable name is flagged by Perl (if you used -w),and a misspelled command generates a run-time exception. An insertion typo becomes dangerous when the typo is still a valid Perl statement but executes an unexpected function.
Note: If you are looking for good and high quality web space to host and run your java application check Vision java hosting services

written by people obsessed with minimizing typing: we

Sunday, October 29th, 2006

written by people obsessed with minimizing typing: we recommend that you insert a semicolon after every statement, even at the end of a block, since you never know when you might come back later and insert another statement after it.) $message = “Hello World”; print “$messagen” exit; Perl responds with syntax error at line 3, next token “exit” The Perl compiler, like every other compiler we know, often goes somewhat further in your program than the actual error before reporting a problem. Since we’ve never met a compiler that indicated an error for a line number preceding the first one containing an error, we can use Perl’s report of the line number as an upper bound and work backwards from it. The actual syntax error in your program could occur not just on the line reported by Perl, but on any line preceding it. In this case, the line it reported was the last line of the file anyway (which often happens), but them’s the breaks. The strategy for fixing this problem: “syntax error” tells you that Perl has determined the problem to be a straightforward typo (count your blessings), so you know what you’re looking for something that doesn’t look like valid Perl, on or before line 3. Perl gives us an additional piece of information, that the next token is exit. If we’re lucky, that means that the error occurred immediately before that word, so that’s where we start looking. Immediately, we notice a missing semicolon on line 2. Success! Problem located and identified. Case closed. (No, there’s no substitute for looking over the code with a Mark I eyeball. However, if you have reams of code to search, you can reduce the amount you have to check by removing or commenting out parts of it selectively until you see the error message change.) Now let’s consider another missing semicolon that’s far more insidious: use strict my $message = “Hello Worldn”; print $message; exit; This program compiles fine; it even runs without error. Unfortunately, it also runs without output. What happened to the contents of $message? A long or a short way can be used to debug this problem. Let’s try the long way first (the reason for this choice will become apparent in due course). We’ll fire up the debugger (see Chapter 7). The first (meaningful) output from the debugger is
Note: If you are looking for reliable and quality webspace company to host and run your servlet application check Actions servlet hosting services

written by people obsessed with minimizing typing: we

Sunday, October 29th, 2006

main::(-:3): print $message; DB<1> Hmm. Something’s wrong here. The debugger halted at the first executable line of code. (A use statement is a compile time directive, so we don’t expect to see that in the debugger.) Why aren’t we seeing the assignment to $message? No wonder when we examine $message there’s nothing there: DB<1> x $message 0 undef For some reason, line 2 isn’t considered executable. It isn’t commented out, so it must be being executed at compile time instead. Hmm again. That rings a bell. Wait a minute, it comes right after a use statement. Could it be that Perl is considering it to be part of the use statement? At this point, we discover the missing semicolon and probably are too relieved to care about why it happened. But for the curious among you, here’s an explanation: the absence of the semicolon and the fact that all white space (including newlines) looks the same to Perl means that lines 1 and 2 parse as use strict my $message = “Hello Worldn”; If we look at the specification for the use statement, we see that it is use Module LIST So the name of the module can be followed by a list and an assignment is a valid member of a list! Because evaluation occurs in the scope of the use statement, my $message= “Hello Perl Worldn”; ends up scoped inside the implicit BEGIN block created by use, and hence disappears by the next line because it was a lexical variable. The debugger demonstration shows the long way to find our typo, but in this case a shorter error analysis method exists. The -wflag produces warnings for the more common Perl programming problems. Run the program with the -w flag and Perl returns Name “main::message” used only once: possible typo at typo.pl line 3. Use of uninitialized value at typo.pl line 3. As we now know, the assignment of $messagehappens in the wrong place due to the typo. The warning alerts us to the fact that when $messageis used, it doesn’t yet contain a defined value. This example should trouble you. If you put usestrictin all your programs (and if you aren’t doing so already, you will by the time you finish this book), you should be wondering now whether the strictness is in effect. To check, remove my from the $messageassignment. If strictness is in effect, Perl will complain that $message lacks an explicit package name.
Note: If you are looking for reliable and quality webspace company to host and run your servlet application check Actions servlet hosting services

Chapter 8. Syntax Errors “Fire bad!” Frankenstein’s monster

Sunday, October 29th, 2006

per hour of programming. If you crack the whip and force people to move more quickly, Humphreys notes, things get even worse. “[The industry] can’t survive with this level of quality,” he adds. As long as we’re being prosaic, let’s go all the way and define a typo. The American Heritage Dictionary (third edition) merely states, “A typographical error” (which is accurate but just a bit too prosaic for our liking). So we’ll just say that you’ve made a typo when the characters that come out on the screen are not the ones that you intended to type. A program containing a typo can still be correct syntactically, even if not semantically, as the legions of typists who rely on the squiggly lines under spelling errors in Microsoft Word have discovered to their chagrin.[2] [2] And don’t expect the squiggly lines made under a clause it considers grammatically incorrect to save you either: Word 97 has no problem with the sentence, “Aye fought eyesore a putty cat.” Syntax simply refers to whether the program obeys the grammar the parser expects; semantics refers to what the program means. A move in a game of chess could be syntactically invalid (such as moving a pawn forward six spaces), or syntactically valid but semantically stupid (such as a castling that puts you a move away from being checkmated). 8.1 Typo Pathologies Later we’ll tackle the more complex issues of errors committed when you typed exactly what you intended to type, but the program still fails to work. This chapter discusses the humble typo: if you could see it, you’d know it was a mistake, and how to fix it. The trick is to find the typo in the first place. We will unfurl an array of techniques for zeroing in on the miscreant. Fortunately, the scope of the typo is not unlimited; seldom does someone type a word entirely different from the one they were thinking of, for instance. The following quote from the Cambridge doctoral thesis of Stephen Moss appositely states the possible choices, although he was referring to the effect of channel errors on textual transmission: 1. Deletion: “The Prime Minister spent the weekend in the country shooting peasants.” 2. Insertion: “The walkway across the trout hatchery was supported on concrete breams.” 3. Alteration: “Say it with glowers”; “For sale: Volvo 144 with overdrive, fuel infection, etc.” 4. Transposition: “Yet, down the road, you will still find the corner shop where the lady behind the counter will lovingly warp your presents.” Let’s consider examples of each category of typo, and we’ll show the strategy we followed to hunt them down and destroy them. 8.1.1 Deletion The most common example of deletion must be the failure to place a semicolon between two statements. (Perl requires semicolons as statement separators, not statement terminators; the difference is that the last statement in a block does not require a trailing semicolon. We present this piece of wisdom to help you understand programs you may inherit that were

Note: If you are looking for good and affordable webspace to host and run your servlet application check Virtualwebstudio servlet hosting services

Chapter 8. Syntax Errors “Fire bad!” Frankenstein’s monster

Sunday, October 29th, 2006

Chapter 8. Syntax Errors “Fire bad!” Frankenstein’s monster in a fit of homicidal frustration “Typos bad!” The first programmer, same emotion We realize we’re tempting scorn by devoting a chapter to a subject as prosaic as typos, but someone must do these dirty jobs. Why? Consider this extract from a shift.com article by Clive Thompson:[1] [1] http://www.shift.com/shiftstd/SiteMap/frames/mag7.6.asp?searchfor=7.6bombsquad Still, by any standard, speed kills quality. Consider the research currently being conducted by Watts Humphrey, a 45-year veteran of the industry and a fellow of the Software Engineering Institute. By studying programmers as they work, he has found that they make one mistake for every seven to ten lines of code a stunning level of errors. And almost one-fifth of those errors are simply typos. All in all, coders introduce bugs at the rate of 4.2 defects

Note: If you are looking for good and affordable webspace to host and run your servlet application check Virtualwebstudio servlet hosting services

7.3.2 ptkdb ptkdb, developed by Andrew E. Page,

Sunday, October 29th, 2006

More than a command line, less than a GUI, the free and insanely powerful GNU text editor includes quasi-graphical support for a simple but intuitive interface to the perl debugger via the cperlmode (see Chapter 3). To start a debug session in the active buffer, enter M-x perldbRET, and you’ll get something like Figure 7-3, where one subwindow contains the source code, and the other is for interactions with the debugger. Figure 7-3. The emacs interface to the Perl debugger via cperl-mode The syntax highlighting is evident in the source code when viewed in color. (Keywords in blue, subroutine prototypes in heavy blue, comments in red, and double-quoted strings in gray; if you don’t like any of those assignments, they are easy to change.) Emacs is available with any Linux system and can be downloaded for virtually any platform from http://www.gnu.org/software/emacs/emacs.html. Don’t overlook the use of debuggers as a teaching tool. I often use ptkdb to monitor the evolution of a variable (scalar, array, hash) as a program executes. Such a demonstration allows a new programer to observe a variable’s value and scope in various situations which is quite useful when you are forced to explain the difference between lexical variables and package variables.
Note: If you are looking for high quality webhost to host and run your jsp application check Vision jsp hosting services

7.3.2 ptkdb ptkdb, developed by Andrew E. Page,

Sunday, October 29th, 2006

7.3.2 ptkdb ptkdb, developed by Andrew E. Page, is a free, Tk-based, graphical interface to the built-in perl debugger, with a clean and intuitive design. Obtain ptkdbfrom http://search.cpan.org/search?dist=Devel-ptkdb. It also requires PerlTk by Nick Ing- Simmons (http://search.cpan.org/search?dist=Bundle-Tk). ptkdbis invoked as a debugger plugin, so to debug myprog.pl, type perl -d:ptkdb myprog.pl There is little we need to add to the description of ptkdb. In Figure 7-2, we see that the current line is highlighted, nonbreakable line numbers are struck through, and the tabbed panes on the right are for inspecting expressions, subroutines, and breakpoints. The appearance of the interface can be extensively customized. Figure 7-2. The ptkdb interface One key advantage of ptkdbis that it can be used to debug a Perl program used in a CGI process. We give examples of this in Chapter 13. 7.3.3 Emacs
Note: If you are looking for high quality webhost to host and run your jsp application check Vision jsp hosting services