[ Pobierz całość w formacie PDF ]

print " $key\t ",$entry{$key}," \n";
}
print ")\n";
}
}
}
}
Note that, even though the subroutine is compiled in package dumpvar, the name of the subroutine is
qualified so that its name is inserted into package "main".
Style
Each programmer will, of course, have his or her own preferences in regards to formatting, but there are
some general guidelines that will make your programs easier to read.
1. Just because you CAN do something a particular way doesn t mean that you SHOULD do it that
way. Perl is designed to give you several ways to do anything, so consider picking the most
readable one. For instance open(FOO,$foo) || die "Can t open $foo: $!"; is better than die "Can t
open $foo: $!" unless open(FOO,$foo); because the second way hides the main point of the
statement in a modifier. On the other hand print "Starting analysis\n" if $verbose; is better than
$verbose && print "Starting analysis\n"; since the main point isn t whether the user typed -v or
not.
Similarly, just because an operator lets you assume default arguments doesn t mean that you have
to make use of the defaults. The defaults are there for lazy systems programmers writing one-shot
programs. If you want your program to be readable, consider supplying the argument.
Along the same lines, just because you can omit parentheses in many places doesn t mean that you
ought to:
return print reverse sort num values array;
return print(reverse(sort num (values(%array))));
When in doubt, parenthesize. At the very least it will let some poor schmuck bounce on the % key
in vi.
Even if you aren t in doubt, consider the mental welfare of the person who has to maintain the
code after you, and who will probably put parens in the wrong place.
2. Don t go through silly contortions to exit a loop at the top or the bottom, when perl provides the
"last" operator so you can exit in the middle. Just outdent it a little to make it more visible:
line:
for (;;) {
statements;
last line if $foo;
next line if /^#/;
statements;
}
3. Don t be afraid to use loop labels--they re there to enhance readability as well as to allow
multi-level loop breaks. See last example.
4. For portability, when using features that may not be implemented on every machine, test the
construct in an eval to see if it fails. If you know what version or patchlevel a particular feature
was implemented, you can test $] to see if it will be there.
5. Choose mnemonic identifiers.
6. Be consistent.
Debugging
If you invoke perl with a -d switch, your script will be run under a debugging monitor. It will halt before
the first executable statement and ask you for a command, such as:
h Prints out a help message.
T Stack trace.
s Single step. Executes until it reaches the beginning of another statement.
n Next. Executes over subroutine calls, until it reaches the beginning of the next statement.
f Finish. Executes statements until it has finished the current subroutine.
c Continue. Executes until the next breakpoint is reached.
c line Continue to the specified line. Inserts a one-time-only breakpoint at the specified line.
Repeat last n or s.
l min+incr
List incr+1 lines starting at min. If min is omitted, starts where last listing left off. If incr is
omitted, previous value of incr is used.
l min-max
List lines in the indicated range.
l line List just the indicated line.
l List next window.
- List previous window.
w line
List window around line.
l subname
List subroutine. If it s a long subroutine it just lists the beginning. Use "l" to list more.
/pattern/
Regular expression search forward for pattern; the final / is optional.
?pattern?
Regular expression search backward for pattern; the final ? is optional.
L List lines that have breakpoints or actions.
S Lists the names of all subroutines.
t Toggle trace mode on or off.
b line condition
Set a breakpoint. If line is omitted, sets a breakpoint on the line that is about to be executed. If a
condition is specified, it is evaluated each time the statement is reached and a breakpoint is taken
only if the condition is true. Breakpoints may only be set on lines that begin an executable
statement.
b subname condition
Set breakpoint at first executable line of subroutine.
d line
Delete breakpoint. If line is omitted, deletes the breakpoint on the line that is about to be executed.
D Delete all breakpoints.
a line command
Set an action for line. A multi-line command may be entered by backslashing the newlines.
A Delete all line actions.

Set an action to happen before every debugger prompt. A multi-line command may be entered by
backslashing the newlines.
> command [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • skierniewice.pev.pl
  •