Perl
From DrugPedia: A Wikipedia for Drug discovery
(→Define Some Variables) |
|||
(50 intermediate revisions not shown.) | |||
Line 1: | Line 1: | ||
- | <center>PERL - Practical Extraction and Report Language</center> | + | '''<center>'''PERL''' - Practical Extraction and Report Language</center>''' |
- | + | A server side-scripting language (programs gets executed on server side) developed in 1986 by Larry Wall, the UNIX based language has evolved into a powerful tool for the internet. It was designed as a quick-fix patch program for UNIX based systems. The language is very simplistic, offering optimum flexibility, perfect for short, straightforward scripting. | |
Since then its popularity has increased due to its flexibility, portability, usefulness, and its varied features. To get started, load a simple text editor program and follow along in our examples. | Since then its popularity has increased due to its flexibility, portability, usefulness, and its varied features. To get started, load a simple text editor program and follow along in our examples. | ||
- | < | + | ==Getting Started== |
- | < | + | First things first, you must have latest version of PERL installed on your web hosting machine available for download via Perl.com, just follow the download links. They also offer installation help for a wide variety of operating systems. We suggest you direct any installation help to the experts there. |
+ | |||
+ | This page will be web based, working with and creating files over the internet. File management is the bread and butter of the PERL language, and as you will discover, it's absolutely perfect for doing so. | ||
+ | |||
+ | ===File Extension=== | ||
+ | A PERL script can be created inside of any normal simple-text editor program. There are several programs available for every type of platform. There are many programs designed for programmers available for download on the web. | ||
+ | |||
+ | Regardless of the program you choose to use, a PERL file must be saved with a .pl (.PL) file extension in order to be recognized as a functioning PERL script. File names can contain numbers, symbols, and letters but must not contain a space. Use an underscore (_) in places of spaces. | ||
+ | |||
+ | ==First Script== | ||
+ | With PERL installed we are ready to dive into our first script. There are a few elements every PERL script must contain in order to function. Open up your favorite simple text editor, the file extension for PERL scripts is .pl. Save your files with this extension. | ||
+ | The first line of every PERL script is a commented line directed toward the PERL interpreter. This line is generally the same from one instal of PERL to the next, it might look something like this: | ||
+ | |||
+ | '''firstscript.pl''' | ||
+ | |||
+ | <nowiki>#!/usr/bin/perl</nowiki> | ||
+ | |||
+ | The comment points to the installation path of PERL, usually /usr/bin/perl. If not, you can locate the directory tree to PERL somewhere in the documentation of your web server, or email your web host and they can specify your PERL installation directory. | ||
+ | |||
+ | ==HTTP Headers== | ||
+ | Because we are working in a web environment we are sort of jumping ahead of the game. We have to introduce some HTTP headers so that PERL understands we are working with a web browser. To do this we have to run another line of strange code called an HTTP header as you may have guessed. It looks something like this: | ||
+ | |||
+ | <nowiki>#!/usr/bin/perl</nowiki> | ||
+ | |||
+ | print "content-type: text/html \n\n"; | ||
+ | |||
+ | At this point our script still has no real functionality, all we have done thus far is locate our PERL interpreter and tell it that we are going to be working with a web browser or in a web environment. | ||
+ | |||
+ | == Hello, PERL! Script== | ||
+ | |||
+ | Now that we have located the interpreter and told PERL we are working with the web, we can print text to the browser using print function. | ||
+ | |||
+ | <nowiki>#!/usr/bin/perl</nowiki> | ||
+ | |||
+ | print "content-type: text/html \n\n"; | ||
+ | |||
+ | print "Hello, PERL!"; | ||
+ | |||
+ | You should see "Hello, PERL!" in the top left corner of your browser, pretty simple and straightforward. | ||
+ | |||
+ | ===Execute Your First Script=== | ||
+ | Now it is time to upload your firstscript.pl to your web server and execute it. After you upload your file be sure to CHMOD the script file and allow anonymous execution priviledge, generally a setting of 755 works perfectly. | ||
+ | |||
+ | You script is working perfectly if you are staring at a blank screen and didn't recieve a 500 or 404 error message. | ||
+ | |||
+ | ===Debugging Your Script(s)=== | ||
+ | If you are using an FTP program to upload your scripts, set the upload type to ASCII or "Text". This setting prevents the mysterious addition of random characters that sometimes happens when copying files across different operating systems. Learning to do this prevents hours of headaches and frustration. | ||
+ | |||
+ | Another great debugging technique is to isolate the code you are currently working on. To do this you can temporarily comment out lines of code to isolate only the section that is returning an error message. | ||
+ | |||
+ | ===Syntax=== | ||
+ | PERL follows a very specific syntax not unlike other programming languages. It is important to develop good syntax habits as it will save you from having to debug things later, not to mention save yourself from eye strain and mind numbing headaches. | ||
+ | |||
+ | === Case Sensitivity=== | ||
+ | File names, variables, and arrays are all case sensitive. If you capitalize a variable name when you define it, you must capitalize it to call it. | ||
+ | |||
+ | A great tip for large scripts containing a vast number of variable names it is best to be consistent with your case sensitivity and maybe even develop a system for naming variables that makes sense to you. For the majority of us programmers, capitals are simply not an option. | ||
+ | |||
+ | $VAriaBLE_NAmES = "string"; | ||
+ | |||
+ | $LIKe_tHESE = "Another String"; | ||
+ | |||
+ | $ARe_HArd_to_Type = "A Third String"; | ||
+ | |||
+ | ===Comments=== | ||
+ | As with any programming language, PERL offers an escape from your code via the '#' sign. Any words, spaces, or marks after a pound symbol will be ignored by the program interpreter, offering you the coder, a chance to place reminders to yourself about your code. It's a great way to note specifics of your code to yourself or others viewing your code/script. Comments are necessary for any script you wish to publish to others or make readily available. | ||
+ | PERL Comment: | ||
+ | |||
+ | <nowiki>#!/usr/bin/perl</nowiki> | ||
+ | |||
+ | print "Content-type: text/html \n\n"; # the header | ||
+ | |||
+ | <nowiki>#Comments start with a #</nowiki> | ||
+ | |||
+ | This comment is extreme and overdone, you might see more comments like this in scripts that are offered free on the internet. Often programmers will include a large commented section as an installation or set-up guide included right there in the script itself. | ||
+ | |||
+ | ===Escaping Characters=== | ||
+ | In PERL we use the backslash (\) character to escape any type of character that might interfere with our code. For example there may become a time when you would like to print a dollar sign rather than use one to define a variable. To do this you must "escape" the character using a backslash (\). | ||
+ | |||
+ | $string = "David paid \$4.34 for Larry\'s shirt."; | ||
+ | |||
+ | $email = "youremail\@youremail.com"; | ||
+ | |||
+ | print "$string"; | ||
+ | |||
+ | print "$email"; | ||
+ | |||
+ | David paid $4.34 for Larry's shirt. | ||
+ | |||
+ | |||
+ | |||
+ | ==Define Some Variables== | ||
+ | A variable is defined by the ($) symbol (scalar), the (@) symbol (arrays), or the (%) symbol (hashes). | ||
+ | |||
+ | <nowiki>#!/usr/bin/perl</nowiki> | ||
+ | |||
+ | print "Content-type: text/html \n\n"; #HTTP HEADER | ||
+ | |||
+ | $myname = "some_value"; | ||
+ | |||
+ | @array = ("value00","value01","value02"); | ||
+ | |||
+ | %hash = ("Quarter", 25, "Dime", 10, "Nickle", 5); | ||
+ | |||
+ | -- OR -- | ||
+ | |||
+ | my $myname = "some string"; | ||
+ | |||
+ | my @array = ("value00", "value01", "value02"); | ||
+ | |||
+ | The latter example using the my parameter is another means to define a variable that you might run across as you gain more experience. It is not necessary to use the my parameter. Variables can be defined either way. | ||
+ | |||
+ | ===Scalar Variables=== | ||
+ | |||
+ | Scalar variables are simple variables containing only one element--a string, a number, or a reference. Strings may contain any symbol, letter, or number. Numbers may contain exponents, integers, or decimal values. The bottom line here with scalar variables is that they contain only one single piece of data. What you see is what you get with scalar variables. | ||
+ | |||
+ | $number = "5"; | ||
+ | |||
+ | $exponent = "2 ** 8"; | ||
+ | |||
+ | $string = "Hello, PERL!"; | ||
+ | |||
+ | $stringpart_1 = "Hello, "; | ||
+ | |||
+ | $stringpart_2 = "PERL!"; | ||
+ | |||
+ | Scalars are very straight forward. Notice that we used a period (.) between each of our variables. This is a special kind of operator that concatenates two or more variables. | ||
+ | |||
+ | ===Array Variables=== | ||
+ | |||
+ | @days = ("Monday", "Tuesday", "Wednesday"); | ||
+ | |||
+ | @months = ("April", "May", "June"); | ||
+ | |||
+ | ===Define A Hash=== | ||
+ | |||
+ | Hashes are complex lists with both a key and a value part for each element of the list. We define a hash using the percent symbol (%). | ||
+ | |||
+ | %coins = ("Quarter", 25, "Dime", 10, "Nickle", 5); | ||
+ | |||
+ | %ages = ("Jerry", 45, "Tom", 22, "Vickie", 38); | ||
+ | |||
+ | Hashes are very complex data types, for now just understand the syntax of how to define one. Later we will take a closer look at these complex variables. | ||
+ | |||
+ | ===Strings=== | ||
+ | |||
+ | Strings are scalar as we mentioned previously. There is no limit to the size of the string, any amount of characters, symbols, or words can make up your strings. | ||
+ | |||
+ | When defining a string you may use single or double quotations, you may also define them with the q subfunction. | ||
+ | |||
+ | $single = 'This string is single quoted'; | ||
+ | |||
+ | $double = "This string is double quoted"; | ||
+ | |||
+ | $userdefined = q^Carrot is now our quote^; | ||
+ | |||
+ | ====Formatting Strings w/ Formatting Characters==== | ||
+ | |||
+ | Strings can be formatted to your liking using formatting characters. Some of these characters also work to format files created in PERL. Think of these characters as miniature functions. | ||
+ | |||
+ | '''Character Description''' | ||
+ | |||
+ | \L Transform all letters to lowercase | ||
+ | |||
+ | \l Transform the next letter to lowercase | ||
+ | |||
+ | \U Transform all letters to uppercase | ||
+ | |||
+ | \u Transform the next letter to uppercase | ||
+ | |||
+ | \n Begin on a new line | ||
+ | |||
+ | \r Applys a carriage return | ||
+ | |||
+ | \t Applys a tab to the string | ||
+ | |||
+ | \f Applys a formfedd to the string | ||
+ | |||
+ | \b Backspace | ||
+ | |||
+ | \a Bell | ||
+ | |||
+ | \e Escapes the next character | ||
+ | |||
+ | \0nn Creates Octal formatted numbers | ||
+ | |||
+ | \xnn Creates Hexideciamal formatted numbers | ||
+ | |||
+ | \cX Control characters, x may be any character | ||
+ | |||
+ | \Q Do not match the pattern | ||
+ | |||
+ | \E Ends \U, \L, or \Q functions | ||
+ | |||
+ | $mystring = "welcome to tizag.com!"; | ||
+ | |||
+ | $newline = "welcome to \ntizag.com!"; | ||
+ | |||
+ | $capital = "\uwelcome to tizag.com!"; | ||
+ | |||
+ | $ALLCAPS = "\Uwelcome to tizag.com!"; | ||
+ | |||
+ | Any combination of these special characters can be used at any time to properly punctuate your strings. They also come in handy when printing out HTML with your PERL functions. | ||
+ | |||
+ | ====Substr() and String Indexing==== | ||
+ | |||
+ | The substr() function allows for the temporary replacement of characters in a string. We can change the string "Hello, PERL" to "Hello, World!" quite easily. Each character of the string is automatically assigned a numeric value by PERL, which means that we can index any of the characters in our strings with this number. PERL counts each character of the string beginning with 0 for the first character and continuing until it reaches the end of a string. | ||
+ | |||
+ | Two arguments must be sent with our substr() function, the string you wish to index and the index number. If two arguments are sent, PERL assumes that you are replacing every character from that index number to the end. | ||
+ | |||
+ | substr($mystring, 7) = "World!"; | ||
+ | |||
+ | Because we only specified one numeric parameter for the string, PERL assumed we wanted to replace every character after the 7th, with our new string. If we throw a third parameter in our function we can replace only a chunk of our string with a new string. | ||
+ | |||
+ | ===Numbers=== | ||
+ | |||
+ | Numbers are scalar data. They exist in PERL as real numbers, float, integers, exponents, octal, and hexidecimal numbers. | ||
+ | |||
+ | $real = 27; | ||
+ | |||
+ | $float = 3.14159; | ||
+ | |||
+ | $integer = -4; | ||
+ | |||
+ | $exponent = 10e12; | ||
+ | |||
+ | [[Perl Conti.....]] |
Current revision
A server side-scripting language (programs gets executed on server side) developed in 1986 by Larry Wall, the UNIX based language has evolved into a powerful tool for the internet. It was designed as a quick-fix patch program for UNIX based systems. The language is very simplistic, offering optimum flexibility, perfect for short, straightforward scripting.
Since then its popularity has increased due to its flexibility, portability, usefulness, and its varied features. To get started, load a simple text editor program and follow along in our examples.
Contents |
[edit] Getting Started
First things first, you must have latest version of PERL installed on your web hosting machine available for download via Perl.com, just follow the download links. They also offer installation help for a wide variety of operating systems. We suggest you direct any installation help to the experts there.
This page will be web based, working with and creating files over the internet. File management is the bread and butter of the PERL language, and as you will discover, it's absolutely perfect for doing so.
[edit] File Extension
A PERL script can be created inside of any normal simple-text editor program. There are several programs available for every type of platform. There are many programs designed for programmers available for download on the web.
Regardless of the program you choose to use, a PERL file must be saved with a .pl (.PL) file extension in order to be recognized as a functioning PERL script. File names can contain numbers, symbols, and letters but must not contain a space. Use an underscore (_) in places of spaces.
[edit] First Script
With PERL installed we are ready to dive into our first script. There are a few elements every PERL script must contain in order to function. Open up your favorite simple text editor, the file extension for PERL scripts is .pl. Save your files with this extension. The first line of every PERL script is a commented line directed toward the PERL interpreter. This line is generally the same from one instal of PERL to the next, it might look something like this:
firstscript.pl
#!/usr/bin/perl
The comment points to the installation path of PERL, usually /usr/bin/perl. If not, you can locate the directory tree to PERL somewhere in the documentation of your web server, or email your web host and they can specify your PERL installation directory.
[edit] HTTP Headers
Because we are working in a web environment we are sort of jumping ahead of the game. We have to introduce some HTTP headers so that PERL understands we are working with a web browser. To do this we have to run another line of strange code called an HTTP header as you may have guessed. It looks something like this:
#!/usr/bin/perl
print "content-type: text/html \n\n";
At this point our script still has no real functionality, all we have done thus far is locate our PERL interpreter and tell it that we are going to be working with a web browser or in a web environment.
[edit] Hello, PERL! Script
Now that we have located the interpreter and told PERL we are working with the web, we can print text to the browser using print function.
#!/usr/bin/perl
print "content-type: text/html \n\n";
print "Hello, PERL!";
You should see "Hello, PERL!" in the top left corner of your browser, pretty simple and straightforward.
[edit] Execute Your First Script
Now it is time to upload your firstscript.pl to your web server and execute it. After you upload your file be sure to CHMOD the script file and allow anonymous execution priviledge, generally a setting of 755 works perfectly.
You script is working perfectly if you are staring at a blank screen and didn't recieve a 500 or 404 error message.
[edit] Debugging Your Script(s)
If you are using an FTP program to upload your scripts, set the upload type to ASCII or "Text". This setting prevents the mysterious addition of random characters that sometimes happens when copying files across different operating systems. Learning to do this prevents hours of headaches and frustration.
Another great debugging technique is to isolate the code you are currently working on. To do this you can temporarily comment out lines of code to isolate only the section that is returning an error message.
[edit] Syntax
PERL follows a very specific syntax not unlike other programming languages. It is important to develop good syntax habits as it will save you from having to debug things later, not to mention save yourself from eye strain and mind numbing headaches.
[edit] Case Sensitivity
File names, variables, and arrays are all case sensitive. If you capitalize a variable name when you define it, you must capitalize it to call it.
A great tip for large scripts containing a vast number of variable names it is best to be consistent with your case sensitivity and maybe even develop a system for naming variables that makes sense to you. For the majority of us programmers, capitals are simply not an option.
$VAriaBLE_NAmES = "string";
$LIKe_tHESE = "Another String";
$ARe_HArd_to_Type = "A Third String";
[edit] Comments
As with any programming language, PERL offers an escape from your code via the '#' sign. Any words, spaces, or marks after a pound symbol will be ignored by the program interpreter, offering you the coder, a chance to place reminders to yourself about your code. It's a great way to note specifics of your code to yourself or others viewing your code/script. Comments are necessary for any script you wish to publish to others or make readily available. PERL Comment:
#!/usr/bin/perl
print "Content-type: text/html \n\n"; # the header
#Comments start with a #
This comment is extreme and overdone, you might see more comments like this in scripts that are offered free on the internet. Often programmers will include a large commented section as an installation or set-up guide included right there in the script itself.
[edit] Escaping Characters
In PERL we use the backslash (\) character to escape any type of character that might interfere with our code. For example there may become a time when you would like to print a dollar sign rather than use one to define a variable. To do this you must "escape" the character using a backslash (\).
$string = "David paid \$4.34 for Larry\'s shirt.";
$email = "youremail\@youremail.com";
print "$string";
print "$email";
David paid $4.34 for Larry's shirt.
[edit] Define Some Variables
A variable is defined by the ($) symbol (scalar), the (@) symbol (arrays), or the (%) symbol (hashes).
#!/usr/bin/perl
print "Content-type: text/html \n\n"; #HTTP HEADER
$myname = "some_value";
@array = ("value00","value01","value02");
%hash = ("Quarter", 25, "Dime", 10, "Nickle", 5);
-- OR --
my $myname = "some string";
my @array = ("value00", "value01", "value02");
The latter example using the my parameter is another means to define a variable that you might run across as you gain more experience. It is not necessary to use the my parameter. Variables can be defined either way.
[edit] Scalar Variables
Scalar variables are simple variables containing only one element--a string, a number, or a reference. Strings may contain any symbol, letter, or number. Numbers may contain exponents, integers, or decimal values. The bottom line here with scalar variables is that they contain only one single piece of data. What you see is what you get with scalar variables.
$number = "5";
$exponent = "2 ** 8";
$string = "Hello, PERL!";
$stringpart_1 = "Hello, ";
$stringpart_2 = "PERL!";
Scalars are very straight forward. Notice that we used a period (.) between each of our variables. This is a special kind of operator that concatenates two or more variables.
[edit] Array Variables
@days = ("Monday", "Tuesday", "Wednesday");
@months = ("April", "May", "June");
[edit] Define A Hash
Hashes are complex lists with both a key and a value part for each element of the list. We define a hash using the percent symbol (%).
%coins = ("Quarter", 25, "Dime", 10, "Nickle", 5);
%ages = ("Jerry", 45, "Tom", 22, "Vickie", 38);
Hashes are very complex data types, for now just understand the syntax of how to define one. Later we will take a closer look at these complex variables.
[edit] Strings
Strings are scalar as we mentioned previously. There is no limit to the size of the string, any amount of characters, symbols, or words can make up your strings.
When defining a string you may use single or double quotations, you may also define them with the q subfunction.
$single = 'This string is single quoted';
$double = "This string is double quoted";
$userdefined = q^Carrot is now our quote^;
[edit] Formatting Strings w/ Formatting Characters
Strings can be formatted to your liking using formatting characters. Some of these characters also work to format files created in PERL. Think of these characters as miniature functions.
Character Description
\L Transform all letters to lowercase
\l Transform the next letter to lowercase
\U Transform all letters to uppercase
\u Transform the next letter to uppercase
\n Begin on a new line
\r Applys a carriage return
\t Applys a tab to the string
\f Applys a formfedd to the string
\b Backspace
\a Bell
\e Escapes the next character
\0nn Creates Octal formatted numbers
\xnn Creates Hexideciamal formatted numbers
\cX Control characters, x may be any character
\Q Do not match the pattern
\E Ends \U, \L, or \Q functions
$mystring = "welcome to tizag.com!";
$newline = "welcome to \ntizag.com!";
$capital = "\uwelcome to tizag.com!";
$ALLCAPS = "\Uwelcome to tizag.com!";
Any combination of these special characters can be used at any time to properly punctuate your strings. They also come in handy when printing out HTML with your PERL functions.
[edit] Substr() and String Indexing
The substr() function allows for the temporary replacement of characters in a string. We can change the string "Hello, PERL" to "Hello, World!" quite easily. Each character of the string is automatically assigned a numeric value by PERL, which means that we can index any of the characters in our strings with this number. PERL counts each character of the string beginning with 0 for the first character and continuing until it reaches the end of a string.
Two arguments must be sent with our substr() function, the string you wish to index and the index number. If two arguments are sent, PERL assumes that you are replacing every character from that index number to the end.
substr($mystring, 7) = "World!";
Because we only specified one numeric parameter for the string, PERL assumed we wanted to replace every character after the 7th, with our new string. If we throw a third parameter in our function we can replace only a chunk of our string with a new string.
[edit] Numbers
Numbers are scalar data. They exist in PERL as real numbers, float, integers, exponents, octal, and hexidecimal numbers.
$real = 27;
$float = 3.14159;
$integer = -4;
$exponent = 10e12;