Total Hit Counter

Showing posts with label Perl Operators. Show all posts
Showing posts with label Perl Operators. Show all posts

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 Operators



Pattern Matching
Result
$a =~ /pat/
match
True if $a contains pattern
$a =~ s/p/r/
substitution
Replace contents of p with r in $a
$a =~ tr/a-z/A-Z/
translation
Translate to corresponding characters
Logical Operators
Result
$a && $b
And
True if $a is true and $b is true
$a || $b
Or
$a if $a is true, otherwise $b
! $a
Not
True if $a is not true
Arithmetic Operators
Result
$a + $b
Add
Sum of $a and $b
$a - $b
Subtract
Difference of $a and $b
$a * $b
Multiply
Product of $a times $b
$a / $b
Divide
Quotient of $a divided by $b
$a % $b
Modulus
Remainder of $a divided by $b
$a ** $b
Exponentiate
$a to the power $b
++$a,$a++
Autoincrement
Add 1 to $a
--$a,$a--
Autodecrement
Subtract 1 from $a
rand($a)
Random
A random number in range 0 .. $a
String Operators
Result
$a . $b
Concatenation
Values of $a and $b as one long string
$a x $b
Repeat
Value of $a strung together $b times
substr($a,$o,$l)
Substring
Substring at offset $o of length $l
index($a,$b)
Index
Offset of string $b in string $a
Assignment Operators
Result
$a = $b
Assign
$a gets the value of $b
$a += $b
Add to
Increase $a by $b
$a -= $b
Subtract from
Decrease $a by $b
$a .= $b
Append
Append string $b to $a
File Test Operators
Result
-r $a
Readable
File name in $a is readable by effective uid
-w $a
Writable
Writable by effective uid
-x $a
Executable
Executable by effective uid
-o $a
Owned
Owned by effective uid
-R $a
Readable
Readable by real uid
-W $a
Writable
Writable by real uid
-X $a
Executable
Executable by real uid
-O $a
Owned
Owned by real uid
-e $a
Exists
File exists
-z $a
Non-zero size
File has non-zero size (returns size in bytes)
-s $a
Zero size
File has zero size
-f $a
Regular file
File is a regular file
-d $a
Directory
File is a directory
-l $a
Symbolic link
File is a symbolic link
-p $a
Named pipe
File is a named pipe (FIFO)
-S $a
Socket
File is a socket
-b $a
Block
File is a block special file
-c $a
Character
File is a character special file
-u $a
UID
File has setuid bit set
-g $a
GID
File has setgid bit set
-k $a
Sticky bit
File has sticky bit set
-T $a
Text file
File is a text file
-B $a
Binary
File is a binary file (opposite of -T)
-M $a
Modify
Age of file (at startup) in days since modification
-A $a
Last Access
Age of file (at startup) in days since last access
-C $a
Inode change
Age of file (at startup) in days since inode change