473,425 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ


Subroutines

By Blair Ireland
Senior Editor, TheScripts.com

Subroutines are extremely important to any programmer. The easiest definition for them would be that they are programs within programs, or "mini-programs". Subroutines allow you to script extremely efficiently, by allowing you to put common actions in them. All you have to do is then call the subroutine to make these actions happen, instead of having to copy and paste them in. This reduces the lines in your script and also makes it easier to read and modify.

Example:

sub hello {
      print "Hello, World...\n";
}

This subroutine would be called in the "Meat" of your program by the following syntax: &hello; On the whole then, a sample, extremely simple perl program would look like the following;

#!/usr/bin/perl
  print "Content-type: text/html\n\n";>\n";
  print "<html><head>\n";  print "<title>Hello, world!</title></head>\n";
  print "<body bgcolor=\"#FFFFFF\" text=\"#000000\">\n";
  print "<h1>Hello, world!</h1>\n";
  print "</body></html>\n";

This would just simple print to your web browser "Hello World" in Headline 1 tags. The second line of the program, after the "shebang" line, has print "Content-type: text/html\n\n";>\n"; on it. This is extremely important. It is this line that tells the web browser that it is sending HTML content to it. Otherwise, without it, your browser would read the html as normal text and not process it properly. You also may notice that before any of the quotes within the html, there is a \ in front of it. This is required to escape the character. Print statements have a set of double quotes to determine the string to be printed. If you put a normal quote (") in the html being printed, that would make the print statement think the string has ended. This will cause your program to have errors and not run. Other characters with importance in double quoted strings are the following; $, @, \. These would require to be backslashed, so they would be \$, \@, \\. Remember this when you make e-mail addresses in your print statements. This will prevent you future headaches for finding the errors.

There is a nice way to get around this little nagging problem though (well, the double quote problem anyway). This involves using a nice little thing we call qq. I will show an example of its use below;

print qq~
  <html><head>
  print <title>Hello, world!</title></head>
  <body bgcolor="#FFFFFF" text="#000000">
  <h1>Hello, world!</h1>
  </body></html>
  ~;

Proper syntax for the use of qq is qq<delimiter>, where <delimiter> is the character you are separating everything by. Instead of using the normal quotes (") as a delimiter to separate everything in a normal print statement, in this case, we are using the ~ character. This can also be applied to variables as well, as in the following;

Instead of

$variable = "<body bgcolor=\"#FFFFFF\" text=\"#000000\">";

you would use

$variable = qq~<body bgcolor="#FFFFFF" text="#000000">~;

This comes in extremely handy when you are dealing with large chunks of HTML code.

« What is Perl? Perl Variables »

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.