Perl Basics Cheatsheet Cheat Sheet

A Programming › Perl cheat sheet in English by Suha

Perl Basics cheatsheet from command line

6,445 views 1,391 downloads 29 Dec 2015 History
3.7 avg · 0 ratings

Functions

Arrays
push @arr, $x
Add to end of array
 
$x = pop @arr;
Remove last element of array, put in $x
 
shift @arr; (See also unshift)
Remove first element of an arrray, put in $x
 
$size = scalar @arr;
Number of things in the array
 
See also: split, join, splice, sort
split string­->a­rray, join array-­>st­ring, delete part of array, sort array in many ways
scalar variables
while (defined ($x=<>)) {code}
False if variable has never been set (or when you try to read past the end of an input file)
 
length($x)
Length of a string
 
chomp(­$line);
Remove a newline at the end of a string
 
$short­=substr ($long, 2, 5)
Characters 3-7 of $long (first char is 0!)
Hashes
@key = keys %hash
The lookup terms in the hash
 
if (exists $hh{“H­sp”}) {...}
See whether hash %hh has a value for key Hsp
Input/­Output and Files
open(H­ANDLE, "­>ou­tfi­le") or die “Can’t open $outfile: $!\n”
Open outfile for writing, and associate it with filehandle HANDLE. Use “
 
print $x; print HANDLE $x;
Prints to standard output (screen), Print to filehandle HANDLE
 
warn “Something wrong\n”;
Prints to standard error (screen)
 
$x=<HA­NDL­E>
Read a line from filehandle HANDLE, put in $x
 
close(­HAN­DLE);
Stop readin­g/w­riting a previously opened file
Exit
exit;
Exits the program
 
die "­Som­ething broke!­\n";
exits the program with error message
>> Operators and Loops
>> Matching and Regular Expres­sions
Source :GreyCampus
Training & Certif­ication in Perl :Perl Certif­ication

Badges

Nice Cheat Sheet Good Cheat Sheet

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.