Perl Miscellaneous Functions
dump LABEL
dump
This causes an immediate core dump. Primarily this is so that you can use the `undump' program to turn your core dump into an executable binary after having initialized all your variables at the beginning of the program. When the new binary is executed it will begin by executing a `goto LABEL' (with all the restrictions that goto suffers). Think of it as a goto with an intervening core dump and reincarnation. If LABEL is omitted, restarts the program from the top. WARNING: any files opened at the time of the dump will NOT be open any more when the program is reincarnated, with possible resulting confusion on the part of perl. See also `-u'. Example:
#!/usr/bin/perl
require 'getopt.pl';
require 'stat.pl';
%days = (
'Sun',1,
'Mon',2,
'Tue',3,
'Wed',4,
'Thu',5,
'Fri',6,
'Sat',7);
dump QUICKSTART if $ARGV[0] eq '-d';
QUICKSTART:
do Getopt('f');
...
eval(EXPR)
eval EXPR
eval BLOCK
eval
EXPR is parsed and executed as if it were a little perl program. It is executed in the context of the current perl program, so that any variable settings, subroutine or format definitions remain afterwards. The value returned is the value of the last expression evaluated, just as with subroutines. If there is a syntax error or runtime error, or a die statement is executed, an undefined value is returned by eval, and `$@' is set to the error message. If there was no error, `$@' is guaranteed to be a null string. If EXPR is omitted, evaluates `$_'. The final semicolon, if any, may be omitted from the expression. Note that, since eval traps otherwise-fatal errors, it is useful for determining whether a particular feature (such as dbmopen or symlink) is implemented. If is also Perl's exception trapping mechanism, where the die operator is used to raise exceptions. If the code to be executed doesn't vary, you may use the eval BLOCK form to trap run-time errors without incurring the penalty of recompiling each time. The error, if any, is still returned in `$@'. Evaluating a single-quoted string (as EXPR) has the same effect, except that the eval EXPR form reports syntax errors at run time via `$@', whereas the eval BLOCK form reports syntax errors at compile time. The eval EXPR form is optimized to eval BLOCK the first time it succeeds. (Since the replacement side of a substitution is considered a single-quoted string when you use the `e' modifier, the same optimization occurs there.) Examples:
# make divide-by-zero non-fatal
eval { $answer = $a / $b; }; warn $@ if $@;
# optimized to same thing after first use
eval '$answer = $a / $b'; warn $@ if $@;
# a compile-time error
eval { $answer = };
# a run-time error
eval '$answer ='; # sets $@
ord(EXPR)
ord EXPR
ord
Returns the numeric ascii value of the first character of EXPR. If EXPR is omitted, uses `$_'.
q/STRING/
qq/STRING/
qx/STRING/
These are not really functions, but simply syntactic sugar to let you avoid putting too many backslashes into quoted strings. The q operator is a generalized single quote, and the qq operator a generalized double quote. The qx operator is a generalized backquote. Any non-alphanumeric delimiter can be used in place of `/', including newline. If the delimiter is an opening bracket or parenthesis, the final delimiter will be the corresponding closing bracket or parenthesis. (Embedded occurrences of the closing bracket need to be backslashed as usual.) Examples:
$foo = q!I said, "You said, 'She said it.'"!;
$bar = q('This is it.');
$today = qx{ date };
$_ .= qq
*** The previous line contains the naughty word "$&".\n
if /(ibm|apple|awk)/; # :-)
rand(EXPR)
rand EXPR
rand
Returns a random fractional number between 0 and the value of EXPR. (EXPR should be positive.) If EXPR is omitted, returns a value between 0 and 1. See also srand().
srand(EXPR)
srand EXPR
srand
Sets the random number seed for the rand operator. If EXPR is omitted, does srand(time).
sprintf(FORMAT,LIST)
Returns a string formatted by the usual printf conventions. The `*' character is not supported.
vec(EXPR,OFFSET,BITS)
Treats a string as a vector of unsigned integers, and returns the value of the bitfield specified. May also be assigned to. BITS must be a power of two from 1 to 32. Vectors created with vec() can also be manipulated with the logical operators `|', `&' and `^', which will assume a bit vector operation is desired when both operands are strings. This interpretation is not enabled unless there is at least one vec() in your program, to protect older programs. To transform a bit vector into a string or array of 0's and 1's, use these:
$bits = unpack("b*", $vector);
@bits = split(//, unpack("b*", $vector));
If you know the exact length in bits, it can be used in place of the *.