Wednesday 9 July 2014

perl cgi examples

Follow steps for perl-cgi:

1. I have all my example code at /home/amit/Documets folder.
2. And we have to point the same to the cgi-folder.

lrwxrwxrwx 1 root root 21 Jul  9 18:22 cgi-bin -> /home/amit/Documents/

amit@laptop:/usr/lib/cgi-bin$ ls -ld perl/
drwxrwxr-x 2 amit amit 4096 Jul  9 18:51 perl/

3. All the perl scripts should have executable permission.

4. On the browser, you can try the following link as example:
http://localhost/cgi-bin/perl/hello.pl


###################
Now few perl script example:


NOTE: The first 2 lines be:

#!/usr/bin/perl

print "Content-type: text/html\n\n";


example 1:

#!/usr/bin/perl
  #
  # The traditional first program.

  # Strict and warnings are recommended.
  use strict;
  use warnings;

  # Print a message.
  print "Content-type: text/html\n\n";
  print "Hello, World!\n";



example 2:

#!/usr/bin/perl

print "Content-type: text/html\n\n";

my $str = <<"EOM";
<html>
<head>
<title>CGI Perl Example</title>
</head>
<body>
<h1>CGI Perl Example</h1>
<p>CGI Perl Example</p>
</body>
</html>
EOM

print $str



example 3:

#!/usr/bin/perl

$a = 10;
$var = <<"EOF";
This is the syntax for here document and it will continue
until it encounters a EOF in the first line.
This is case of double quote so variable value will be
interpolated. For example value of a = $a
EOF
print "$var\n";

$var = <<'EOF';
This is case of single quote so variable value will not be
interpolated. For example value of a = $a
EOF
print "$var\n";



example 4:

#!/usr/bin/perl

use strict;
use warnings;

my $text = "A useful tool in natural language processing is concordance. This allows a specific string to be displayed in its immediate context whereever it appears in a text. For example, a concordance program identifying the target string the might produce some of the following output. Notice how the occurrences of the target string line up vertically. ";

my @values = split(/ /, $text);

#print "$values[0]\n"

foreach my $val (@values) {
    print "$val\n"
}

exit 0;


No comments:

Post a Comment