About Me

Friday 20 April 2012

PERL - Syntax

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<br />";
print "$email<br />";
print '$string and $email';

escapecharacters.pl:

David paid $4.34 for Larry's shirt.
youremail@youremail.com
$string and $email

0 comments:

Post a Comment