Javascript toggle visibility

A simple snippet of a JavaScript function to toggle visibility of an element called by ID.

<script type="text/javascript">
<!--
function toggle_visibility(id) {
  var e = document.getElementById(id);
  if(e.style.display == 'block')
          e.style.display = 'none';
  else
          e.style.display = 'block';
}
//-->
</script>

Perl CGI: printing an image

Sometimes you want to incorporate a dynamically generated image into a web page, like this:

<img src="scripts/counter.pl?title=MySite" />

The barebone of this program looks like:

#!/usr/bin/perl
use GD;
use CGI qw/:standard/;

print header(-type=>'image/png');
# Allocate the image and the colors we need
my $im = new GD::Image($imagew, $imageh);
my $grey  = $im->colorAllocate(230,230,230);
my $white = $im->colorAllocate(255,255,255);
my $black = $im->colorAllocate(  0,  0,  0);

# Draw the picture...
$im->line(2,       $topy, $rightx, $topy, $black);
$im->line(2,       $boty, $rightx, $boty, $black);
$im->line($leftx,  2,     $leftx,  $boty, $black);
$im->line($rightx, 2,     $rightx, $boty, $black);
#Print image    
print $im->png;

Perl CGI: output from a web-server

This simple template shows how to create a Perl script to print output to a web browser, and how to get parameters. It’s important to make the script executable before invoking it. The “param” function works both with parameters passed via POST and via GET.

Tested with DreamHost (no particular directory to store script, like old ‘cgi-bin’).

#!/usr/bin/perl
use CGI qw(:standard);
$name  = param('username')  || 'anonymous';
$code  = param('usercode')  || '0000';
print "Content-type: text/html\n\n";
# elaborate data...
# print output (in HTML) 

Hello world! (in Perl)

Here is the script.

#!/usr/local/bin/perl
# Program to do the common "Hello World"
print 'Hello world.';    # This line prints a message

What’s in this?

Every perl program starts off with this as its very first line:

#!/usr/bin/perl

although this may vary from system to system. This line tells the machine what to do with the file when it is executed (ie it tells it to run the file through Perl). This is not a Perl instruction, but is for (Linux) shell. Write #! followed by the path of the perl program. If you are unsure, write the command whereis perl at the shell prompt, then copy and paste the path.

Comments can be inserted into a program with the # symbol, and anything from the # to the end of the line is ignored (with the exception of the first line). The only way to stretch comments over several lines is to use a # on each line. Everything else is a Perl statement which must end with a semicolon, like the last line above.

The print function outputs some information. In the above case it prints out the the literal string Hello world. and of course the statement ends with a semicolon. You may find the above program produces an slightly unexpected result. So the next thing to do is to run it!

Save it as hello.pl (that is the commonly used extension for Perl scripts). Then you can run it like this:$ perl hello.plIn this case the first line is not required, as you invoke perl by yourself. But you can also make it executable and then launch it calling only its file name. In this case the first line is required. So let’s try:

$ chmod 755 hello.pl
$ ./hello.pl