Show Menu
Cheatography

Perl Basics Cheatsheet Cheat Sheet by

Perl Basics cheatsheet from command line

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
   
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Web Programming Cheat Sheet
          Perl Reference Card Cheat Sheet
          perlcheat Cheat Sheet

          More Cheat Sheets by Suha