Skip navigation.
Home
Your source for Perl tips, howto's, faq and tutorials

encode and decode url strings

( categories: )

URL addresses only accepts alphanumeric characters and some punctuation symbols, like parenthesis and underscore.

If you need to use any other symbol (like space) you have to URL encode it using a percent sign followed by the two hexadecimal digits that represents that digit in the ASCII table.

For example, the space symbol is character 32 (hexadecimal 20) in the ASCII table, so it's expressed as %20.

In Perl, the easiest way to URL encode a string is to use uri_escape() function from URI::Escape module. This function converts all the unsafe symbols of a string to its URL encode representation.


file uploading using a web form

( categories: )

Use the functions provided by the CGI module. You should define the form with 'start_multipart_form()' and the upload field with 'filefield()'. The file is uploaded to a temporary location; you have to write to another location in order to keep it.

Have a look at the following example to see a working script:

Example:


redirect error messages to the browser

( categories: )

To send the output of die() and/or confess() to the browser, import the CGI::Carp subroutine 'fatalsToBrowser' to your program.

Example:

#!/usr/bin/perl
 
use CGI::Carp qw(fatalsToBrowser);
 
open(FILE, "nonexistantfile.txt") or die("Can't open file: $!\n");

By default, 'fatalsToBrowser' prints a message at the bottom of the page telling to contact the webmaster; if you want to change that line, you can do it using the 'set_message' routine.


check web links

( categories: )

To verify the status of a web site, use the head function from LWP::Simple module.

In scalar context, head returns true if the url passed as a parameter is accessible.

Example:

#!/usr/bin/perl
 
use LWP::Simple;
 
if ( head("http://www.perlhowto.com") )
{
  print "perlhowto.com is running!\n";
}
else
{
  print "Oops! site is down\n";
}


decode (and encode) HTML character entities

( categories: )

Some special symbols are frequently specified in HTML documents using HTML entities, this entities take the form &name;, like © for the copyright character.

In addition, any character can be specified in HTML in the form of numerical entities, that take the form &#number;, like © for the copyright character.

To convert this kind of HTML data to/from standard characters, you can use the functions 'decode_entities' and 'encode_entities' from HTML::Entities module.

Example:


download a file from the internet

( categories: )

Use the function 'getstore' from the LWP::Simple module. The return value of the function is the HTTP response code.

Example:

#!/usr/bin/perl
 
use LWP::Simple;
 
#-- fetch the zip and save it as perlhowto.zip
my $status = getstore("http://www.perlhowto.com/archive.zip", "perlhowto.zip");
 
if ( is_success($status) )
{
  print "file downloaded correctly\n";
}
else
{
  print "error downloading file: $status\n";
}


useful modules to develop web related applications

( categories: | )

- CGI
General purpose module for creating web pages

- Template
Template Toolkit - excellent template system used primarily for generating dynamic web content

- LWP
Very useful to fetch web contents

- WWW::Mechanize
Very helpful module to automate interaction with a website


Syndicate content