Total Hit Counter

Thursday, March 6, 2014

Per-filehandle Special Variables

These variables never need to be mentioned in a local()because they always refer to some value pertaining to the currently selected output filehandle - each filehandle keeps its own set of values.
Variable
Contents
Mnemonic
$|
If set to nonzero, forces a flush after every write or print
When you want your pipes to be piping hot
$%
Current page number
% is page number in nroff
Current page length
#NAME?
$-
Number of lines left on the page
lines_on_page - lines_printed
$~
Name of the current report format
Closely related to $^
$^
Name of the current top-of-page format
Points to top of page
These variables that are always local to the current block, so you never need to mention them in a local(). All of them are associated with the last successful pattern match.
Variable
Contents
Mnemonic
$1..$9
Contains the subpattern from the corresponding set of parentheses in the last pattern matched
like \1..\9
$&
Contains the string matched by the last pattern match
like & in some editors
$`
The string preceding whatever was matched by the last pattern match, not counting patterns matched in nested blocks that have been exited already.
` often precedes a quoted string in normal text
$'
The string following whatever was matched by the last pattern match, not counting patterns matched in nested blockes that have been exited already. For example:
' often follows a quoted string in normal text
   $_ = 'abcdefghi';
   /def/; 
   print "$`:$&:$'\n";    # prints abc:def:ghi
$+
the last bracket matched by the last search pattern. This is useful if you don't know which of a set of alternative patterns matched. For example:
be positive and forward looking
    /Version: (.*)|Revision: (.*)/ && ($rev = $+);
Global Special Variables
Variable
Contents
Mnemonic
$_
The default input and pattern-searching space.
underline is understood to be underlying certain undertakings
$.
The current input line number of the last filehandle that was read. Rember that only an explicit close on the filehandle resets the line number.
many programs use . to mean the current line number
$/
The input record separator, newline by default. $/ may be set to a value longer than one character in order to match a multi-character delimiter. If $/ is undefined, no record separator is matched, and <FILEHANDLE>will read everything to the end of the current file.
/ is used to delimit line boundries when quoting poetry. Or, if you prefer, think of mad slashers cutting things to ribbons.
$\
The output record separator for the print operator.
You set $\ instead of adding \n at the end of the print.
$,
The output field separator for the print operator.
What is printed when there is a , in your printstatement
$"
This is similar to $, except that it applies to array values interpolated into a double-quoted string (or similar interpreted string). Default is space.
Obvious, I think
$#
The output format for numbers display via the printoperator
# is the number sign
$$
The process number of the Perl running this script
Same as shells
$?
The status returned by the last pipe close, backtick(``) command or system operator. Note that this is the status word returned by the wait() system call, so the exit value of the subprocess is actually ($? >>*)$? & 255 gives which signal, if any, the process died from, and whether there was a core dump.
Similar to sh and ksh
$*
Set to 1 to do multi-line matching within a string, 0 to tell Perl that it can assume that strings contain a single line, for the purpose of optimizing pattern matches. Default is 0
* matches multiple things
$0
Contains the name of the file containing the Perl script being executed. Depending on your OS, it may or may not include the full pathname.
Same as sh and ksh
$[
The index of the first element in an array, and of the first character in a substring.
[ begins subscripts
$]
The first part of the string printed out when you say perl -v. It can be used to determine at the beginning of a script whether the Perl interpreter executing the script is in the right range of versions. If used in a numeric context, $] returns version + patchlevel /1000.
Is this version of Perl in the "rightbracket"?
$;
The subscript separator for multi-dimensional array emulation. If you refer to an associative array element as:
Comma (the syntactic subscript separator) is a semi-semicolon. Yeah, it's pretty lame, but $, is already taken for something more important.
   $foo{$a,$b,$c} 
it really means: 
   $foo{join($;, $a, $b, $c)} 
but don't put 
   @foo{$a,$b,$c} 
which means 
   ($foo{$a},$foo{$b},$foo{$c})
$!
If used in a numeric context, yields the current value oferrno, with all the usual caveats. (This means that you shouldn't depend on the value of $! to be anything in particular unless you've gotten a specific error return indicating a system error.) If used in a string context, yields the corresponding sysem error string.
What just went bang?
$@
The Perl syntax error or routine error message from the last eval, do-FILE, or require command. If set, either the compilation failed, or the die function was executed within the code of the eval.
Where was the syntax error at?


Perl Regular Expressions

Result
.
Matches any character except newline
[a-z0-9]
Matches any single character of set
[^a-z0-9]
Matches any single character not in set
\d
Matches a digit, same as [0-9]
Matches a non-digit, same as [^0-9]
\w
Matches an alphanumeric (word) character [a-zA-Z0-9_]
\W
Matches a non-word character [^a-zA-Z0-9_]
\s
Matches a whitespace character (space, tab, newline...)
\S
Matches a non-whitespace character


\n
Matches a newline
\r
Matches a return
\t
Matches a tab
\f
Matches a formfeed
\b
Matches a backspace (inside [] only)
\0
Matches a null character
\000
Also matches a null character because ...
\nnn
Matches an ASCII character of that octal value
\xnn
Matches an ASCII character of that hexadicimal value
\cX
Matches an ASCII control character
Matches the character itself (\|,\.,\*...)


(abc)
Rembers the match for later backreferences
\1
Matches whatever the first set of parens matched
\2
Matches whatever the second set of parens matched
\3
and so on...


x?
Matches 0 or 1 x's, where x is any of the above
x*
Matches 0 or more x's
x+
Matches 1 or more x's
x{m,n}
Matches at least m x's but nore more than n


Matches all of a, b, and c in order
fee|fie|foe
Matches one of fee, fie, or foe


\b
Matches a word boundry (outside [ ] only)
\B
Matches a non-word boundry
^
Anchors match to the beginning of a line or string
$
Anchors match to the end of a line or string