December 19, 2014
Hot Topics:

Building WML Gadgets: A Calendar

  • December 20, 2002
  • By Steve Schafer
  • Send Email »
  • More Articles »

Testing the Rudimentary Script

This script cannot be used to output directly to a device, since it doesn't pass the correct WML header. However, using the interpreter, we can output to the console and paste the results into the Openwave SDK IDE (or other simulator). Using the command "perl cal.pl" at a Unix command shell runs the script and outputs the WML code. This code is then pasted into the Openwave IDE and passed to the simulator as shown in the figure below:


Click for larger image

FIGURE 11.4

The code is pasted into the Openwave IDE and passed to the simulator.

Now that we have a working prototype, let's test a few other dates by adding command-line arguments. We replace the following lines:

# Get current day/month/year
($year,$month,$day) = Today([$gmt]);

with a more extensive routine to parse a command line:

if (@ARGV) {
  $month = (shift @ARGV);
  $year = (shift @ARGV);
} else {
  # Get current day/month/year
  ($year,$month,$day) = Today([$gmt]);
}

This routine determines whether there are command-line parameters; if so, it puts the first parameter into the $month variable and the second into the $year variable. If there are no parameters, the routine uses the current date. Now we can test other months by passing the appropriate values to the script. For example, to test April 2001, we would use the following command line:

perl cal.pl 4 2001

Note: The first few times I tested this script it failed on assorted months—months where the first day is a Sunday, or the last day is a Saturday. Getting the logic down to handle pre-and-post filling of the week rows took a while. When testing a script such as this, it is important to test the most diverse conditions possible.

Almost at the Finish Line

To finish the script, we can add a handful of features;

  • Code to pass the proper header to the browser
  • Support for name/value pair passing via HTTP
  • A mechanism to move backward and forward by month
Header Passing

Passing the header to the browser is easily accomplished with the use of another library, CGI.pm, available from several sources on the Web. (I got my copy from L. Stein at http://stein.cshl.org/WWW/software/CGI/.) This library includes a "header" function for passing HTTP-compliant headers to browsers. By adding these two lines (in appropriate positions) we achieve our goal:

use CGI qw(:standard);
print header(-type=>'text/vnd.wap.wml');
Parameter Handling

Parameter handling is a bit more involved, requiring more parsing code for handling arguments. We need support for name/value pairs to control the script through HTTP calls, but don't want to discard the command-line parameters—they are still valuable for testing.

The following snippet handles command-line parameters as well as name/value pairs and will substitute the current date if either returns an invalid argument:

# Parse command line or name/value pairs
# Command line:
if (@ARGV) { 
  $month = (shift @ARGV);
  $year = (shift @ARGV);

} else {

  # Name/value pairs
  if ($ENV{REQUEST_METHOD} eq 'GET') {
    foreach $input (split("&",$ENV{QUERY_STRING})) {
      if ($input =~ /(.*)=(.*)/) {
        ($key,$value) = ($1, $2);
        # Add keyword/value pair to a list
        $inputs{$key} = $value;
        $month = $inputs{month};
        $year = $inputs{year};
      }
    }
  }

}

# If we don't have a valid date from params,
#   set date to today (server clock)
if (!check_date($year,$month,1)) {
  ($year,$month,$day) = Today([$gmt]);
}

Note: Since we control current access to the script, we can dictate that the parameters follow a certain format:

  • Command-line parameters are numeric month followed by numeric year
  • Name/value pairs are lowercase names ("month" and "year", respectively)
  • Name/value pairs appear in that order
  • Names are paired with numeric values.

The code above does very little input checking other than to default to the current date if something is amiss.





Page 4 of 5



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel