Monday, December 24, 2007
PERL - First Script
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.
PERL - 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:
firstscript.pl:
#!/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.
PERL - 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 with the print function.
helloperl.pl:
#!/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.
PERL - 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 0755 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.
PERL - 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.
PERL - 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.
PERL - 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.
casesensitivity.pl:
$VAriaBLE_NAmES = "string";
$LIKe_tHESE = "Another String";
$ARe_HArd_to_Type = "A Third String";
PERL - 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.
PERL - 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 (\).
escapecharacters.pl:
#!/usr/bin/perl
print "Content-type: text/html \n\n"; #HTTP HEADER
#CREATE STRINGS WITH ESCAPING CHARACTERS
$string = "David paid \$4.34 for Larry\'s shirt.";
$email = "youremail\@youremail.com";
#PRINT THE STRINGS
print "$string
";
print "$email
";
print '$string and $email';
escapecharacters.pl:
David paid $4.34 for Larry's shirt.
youremail@youremail.com
$string and $email
PERL - Define Some Variables
A variable is defined by the ($) symbol (scalar), the (@) symbol (arrays), or the (%) symbol (hashes).
Here is what each type of variable should look like inside of a script.
definevariables.pl:
#!/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");
my %hash =
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.
The doRSA method
This method can be used to encrypt or to decrypt the input string depending on whether the exponent is an encryption key or a decryption key.
The method is hard coded to apply the algorithm for a fixed block size of four characters. Thus, this is not a general-purpose RSA method that can be applied for different block sizes.
(I will provide a more general-purpose version of the method that allows for different block sizes in the next program.)
Let's see some code
Once again setting the main method aside temporarily, Listing 20 shows the beginning of the doRSA method.
String doRSA(String inputString,
BigInteger exp,BigInteger n){
BigInteger block;
BigInteger output;
String temp = "";
String outputString = "";
Listing 20
The code in Listing 20 declares and initializes some variables that will be used later.
Process one block at a time
Listing 21 shows the beginning of a for loop that iterates and processes the incoming string by subdividing the string into one block of four characters during each iteration.
for(int cnt = 0; cnt < inputString.length();
cnt += 4){
temp = inputString.substring(cnt,cnt + 4);
block = new BigInteger(temp);
Listing 21
The code in Listing 21 gets the next block of four characters and uses them to initialize a new BigInteger object that treats the four character substring as an integer.
(This is where the program gets into trouble and throws an exception if the length of the incoming string is not divisible by 4. In that case, the partial block at the end causes the invocation of the substring method to throw a StringIndexOutOfBoundsException.)
spip loops
Basic Syntax
The basic syntax for a loop is as follows:
* Code HTML + balises SPIP
We saw, in the overview of loops and tags, that the HTML code + SPIP’s tags of the above example are repeated as many times as the loop can extract items from the database (in other words, once, several times, or not at all).
The main instruction here is:
- The word BOUCLE is an instruction given to Spip to define a new loop and cannot be modified: all Spip loops must start with the command BOUCLE.
- The n element is the loop’s identifier. It can be either a name or a number and is chosen by the webmaster for every loop he or she makes. We will see later how it is possible to use several loops in the same template. This is after all the object of the exercise, and because of this it is essential for each loop to have a unique identifier.
If you decide to number your loops, the syntax for loop 5 would be:
...
If you decide to give a name to your loops, which is usually more convenient and makes your code easier to read, then the name must start with the underscore symbol “_”. For example:
...
- (TYPE) is a key element defining what kind of items are to be displayed. The syntax is important: the TYPE must be entered between parenthesis, with no spaces, in capital letters, and must be one of the types specified by Spip: ARTICLES, RUBRIQUES, AUTEURS, BREVES, etc. (they are all covered in this documentation).
So, continuing with the previous example, we now have:
...
date validation in javascript
Description: This script validates a date field to ensure it's in the format mm/dd/yyyy. It also intelligently checks that the date ranges are valid, so something like 02/30/2005 is caught.
Step 1: Cut and paste the below script into the
section of your page:/**--------------------------
//* Validate Date Field script- By JavaScriptKit.com
//* For this script and 100s more, visit http://www.javascriptkit.com
//* This notice must stay intact for usage
---------------------------**/
function checkdate(input){
var validformat=/^\d{2}\/\d{2}\/\d{4}$/ //Basic check for format validity
var returnval=false
if (!validformat.test(input.value))
alert("Invalid Date Format. Please correct and submit again.")
else{ //Detailed check for valid date ranges
var monthfield=input.value.split("/")[0]
var dayfield=input.value.split("/")[1]
var yearfield=input.value.split("/")[2]
var dayobj = new Date(yearfield, monthfield-1, dayfield)
if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
alert("Invalid Day, Month, or Year range detected. Please correct and submit again.")
else
returnval=true
}
if (returnval==false) input.select()
return returnval
}
learn French
| J'ai froid. I'm cold. | Je suis en retard! I'm late! |
TYPES OF CRYPTOGRAPHIC ALGORITHMS
Public Key Cryptography: Uses one key for encryption and another for decryption