Net::SAJAX 0.102

In the short time since the first release of Net::SAJAX, there has been three more releases. Version 0.100 changed the call method to now return native Perl data structures instead of JE objects, and so some quirks with using the JE objects as Perl data structures were no more. Version 0.102 introduced a new attribute to the Net::SAJAX object called autoclean_garbage that when enabled (it is disabled by default) will attempt to work around any bad programming on the remote SAJAX page (like PHP warnings appearing before the SAJAX response).

One of the most useful new features in 0.102 is that now all errors are Net::SAJAX::Exception objects. The benefit of this is the ability to handle errors even better.

use 5.008003;
use strict;
use warnings 'all';
 
use Carp qw(croak);
use English qw(-no_match_vars);
use Net::SAJAX;
use Scalar::Util qw(blessed);
 
my $sajax = Net::SAJAX->new(
  url => 'http://example.net/directory.php',
);
 
# Look up a person in the directory
my $people = eval {
  $sajax->call(
    function  => 'SearchDirectory',
    arguments => [$first_name, $last_name],
  );
};
 
if ($EVAL_ERROR) { # Remember, $EVAL_ERROR cam from the English module
  # An error occurred
  if (blessed $EVAL_ERROR && $EVAL_ERROR->isa('Net::SAJAX::RemoteError')) {
    # The server sent an error message, like "Access Denied"
    show_error($EVAL_ERROR->message);
  }
  elsif (blessed $EVAL_ERROR && $EVAL_ERROR->isa('Net::SAJAX::JavaScriptEvaluation')) {
    # Some error occurred while evaluating the JavaScript
    # Perform some clean up to the JS because you know that the
    # site returns bad JS sometimes.
    $people = my_own_js_eval($EVAL_ERROR->javascript_string);
  }
  else {
    # Don't know what this error is, so throw it again
    croak $EVAL_ERROR;
  }
}
 
print "Found the following people:\n";
 
# Loop through each person and print
foreach my $person (@{$people}) {
  printf "%s, %s: %s\n",
    $person->{last_name   },
    $person->{first_name  },
    $person->{phone_number};
}
 
sub my_own_js_eval {
  my ($javascript) = @_;
 
  # Do something with the JavaScript here
 
  return [];
}
sub show_error {
  my ($message) = @_;
 
  print {*STDERR} "Received $message from the server!\n";
 
  exit 1;
}
 
exit 0;

Leave a Reply