![]() |
| ||||||||||||||||||||||||||||||||||||
|
This is the basics of Perl.This is quick look at perl and some references you should know and some scripts.
Contents:BasicsWhere can you get Perl? Perl is free cpan.org
Some of the basics about perl. Perl extensions are .pl and .pm, like
HTML files/pages are .html.
If running on Linux: If running on Win32: Scalar Variables - holds a single value, string or number starts with $ Arrays - hold lists, starts with @
Hashes (perfect)- hold elements with Keys and values, starts with a % chomp, you will most likely be pulling some input in, you will pretty much want to chomp everything you take in. This will remove extra spaces, carriage returns, line feeds and junk.Key Value Key1 => value1 Key2 => value2 print "give me some input"; $response = Running Perl in webpagesIf you ftp something from windows to unix run and the perl code runs fine via command line, but you have issues (error 500) when trying to see the webpage run the dos2unix filename command.
print "Content-type: text/html\n\n"; This tells perl that the information is html and will run in a webbrowser. Most likely if you are going to run perl in a webpage you will be passing it information from an html page. Here is a quick few lines to parse the information out of a post or get from the HTML form to be used in the format of "$FORM{'variable'}"; $stuff = <STDIN>; $holdme = $stuff; print "Content-type: text/html\n\n"; @fields = split(/&/,$stuff); foreach $field (@fields) { ($name, $value) = split(/=/, $field); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; #stop people from using subshells to execute commands $value =~ s/~!/ ~!/g; $FORM{$name} = $value; } Expressions (what does that mean?)m// is matching operator qualifiers for matching
\d digit[0-9] \w word character [A-Za-z0-9] \s whitespace [\f\t\n\r] [^\d] [^\w] [^\s] or \D \W \S - negating the shortcutsMore info can be found at: ArraysThe following is a piece of code that will bring in a file and put each line of the file into an array. It will then check each line for a matching string. If the string is there it will print the next 5 lines.open FH, File_I_Want_To_Read or die "Failed to open file"; @lines = FunctionsCalling functions:This will look for a system function if there is not one locally. myfunction(); sub myfunction { blah blah } This will look for the local function &myfunction; sub myfunction { blah blah } if you have the funtion you want to use stored in a variable you can do a: &$myfunction; sub myfunction { blah blah } LoopsIf you want to break out of a loop use last To continue use nextSplicingprint @lines[$start..$start+6]; This will print an array from $line[$start] to $line[$start+6] Running system tools/commandsIn order to run some operating system commands you need to use back ticks like to get the OS of a machine or machine name do the following:chomp($OS = `uname` ); #Get OS...Linux, AIX, HP, etc chomp($NAME = `uname -n`) #Get machine name, hostnameIf you want to use a file use an easy the following commands: open (FH, ">File.txt");or open (FH, ">>File.txt");The filehandle (FH) will be whatever you use to reference that file in the future. > will rewrite the file >> will append to the file The close the file with the following. close FH;If you want to pull files from a directory you are in, say all the .jpg files, use glob to pull them in an array. @Pics=glob "*.jpg";If you want to pull picture files from a directory and rename them you can use the following routine. @Pics=glob "*.jpg"; $i=1; foreach $Pic(@Pic){ rename $Pic,($i).".jpg"; $i++; }
Executing command lineAt times you will need to run a command from the command line and get the output you can do that with : $source = "\"C:\\Documents and Settings\\markparz\\Desktop\\My Stuff\\Perl\\PhotoTool\\test\\*.jpg\""; $des = "\"C:\\Documents and Settings\\markparz\\Desktop\\My Stuff\\Perl\\PhotoTool\\thumb\""; $EZ = "\"C:\\Program Files\\Easy Thumbnails\\EzThumbs.exe\" $source /D=$des "; print "Getting files from " . $source . "\n"; print "sending them to " . $des . "\n"; print "using command " . $EZ; system($EZ); If you want to execute a command and have the program exit, substitute exec with system.
|