Perl For Dummies

Perl enables you to write powerful programs right from the start, whether you're a programming novice or expert. Perl offers the standard programming tools — comparison operators, pattern-matching quantifiers, list functions — and has shortcuts for inputting character ranges. Perl also offers file tests so you can find what you want fast.






>


>


The Most Useful File Tests in Perl


Programming with Perl is fairly straightforward, which runs to the letters you use for file tests. For example, r tests whether a file can be read, and T looks for a text file. Here are most useful file tests in Perl:



















































TestDescription
-eFile exists.
-rFile can be read.
-wFile can be written to.
-zFile is exactly zero bytes long.
-dNamed item is a directory, not a file.
-TFile is a text file. (The first chunk of a file is examined,
and it’s a text file if fewer than 30 percent or so of the
characters are nonprintable.)
-BFile is a binary file. (This is the exact opposite of the -T
test — it’s a binary file if more than 30 percent or so
of the characters are nonprintable.)
-sSize of the file in bytes.
-CCreation age of file.
-AAccess age of file.
-MModification age of file.




>



>


>


Special Characters in Perl


Like any programming language, Perl uses special commands for special characters, such as backspaces or vertical tabs. So, if you need to program in a bell or a beep or just a carriage return, check the following table for the character that will produce it:







































CharacterMeaning
\nNewline
\rCarriage return
\tTab character
\fFormfeed character
\bBackspace character
\vVertical tab
\aBell or beep
\eEscape character




>



>


>


Perl True-False Comparison Operators


When you're programming with Perl — or any other language — you use comparison operators all the time. The following table shows the common comparisons for Perl in both math and string form:






































ComparisonMathString
Equal to==eq
Not equal to!=ne
Less than<lt
Greater than>gt
Less than or equal to<=le
Greater than or equal to>=ge




>



>


>


Common List Functions in Perl


Perl was originally designed to help process reports more easily. Reports often contain lists, and you may want to use Perl to perform certain functions within a list. The following table shows you common list functions, their splice equivalents, and explains what the function does:




























Functionsplice EquivalentWhat It Does
push (@r, @s)splice(@r, $#r+1,0, @s)Adds to the right of the list
pop (@r)splice(@r, $#r, 1)Removes from the right of the list
shift (@r)splice(@r, 0, 1)Removes from the left of the list
unshift (@r, @s)splice(@r, 0, 0,@s)Adds to the left of the list




>



>


>


Shortcuts for Character Ranges in Perl


You're programming along in Perl and want to use a code shortcut to represent anything from a number to a non-number to any letter or number. You're in luck, because the following table gives you the code, shows you what it’s a shortcut for, and describes it.






































CodeReplacesDescription
\d[0..9]Any digit
\w[a-zA-Z_0-9]Any alphanumeric character
\s[ tnrf]A whitespace character
\D^[0..9]Any non-digit
\W^[a-zA-Z_0-9]Any non-alphanumeric character
\S^[ tnrf]A non-whitespace character




>



>


>


Perl Pattern-Matching Quantifiers


Perl enables you to use common symbols to instruct the program you're writing to match data once, never, or up to a certain number of times. The following table shows you which symbol to use to get the match you want:































SymbolMeaning
+Match 1 or more times
*Match 0 or more times
?Match 0 or 1 time
{n}Match exactly n times
{n,}Match at least n times
{n,m}Match at least n, but not more than m, times (these values must
be less than 65,536)




>






>
dummies


Source:http://www.dummies.com/how-to/content/perl-for-dummies-cheat-sheet.html

No comments:

Post a Comment