CGI-Ajax
view release on metacpan or search on metacpan
lib/CGI/Ajax.pm view on Meta::CPAN
Perl subroutine and returns the results to dest1 and dest2.
=head2 4 Usage Methods
=over 4
=item 1 Standard CGI::Ajax example
Start by defining a perl subroutine that you want available from
javascript. In this case we'll define a subrouting that determines
whether or not an input is odd, even, or not a number (NaN):
use strict;
use CGI::Ajax;
use CGI;
sub evenodd_func {
my $input = shift;
# see if input is defined
if ( not defined $input ) {
return("input not defined or NaN");
}
# see if value is a number (*thanks Randall!*)
if ( $input !~ /\A\d+\z/ ) {
return("input is NaN");
}
# got a number, so mod by 2
$input % 2 == 0 ? return("EVEN") : return("ODD");
}
Alternatively, we could have used coderefs to associate an
exported name...
my $evenodd_func = sub {
scripts/CGI-Application-Ajax-ex03.pl view on Meta::CPAN
sub start {
my $self = shift;
my $evenodd_func = sub {
my $input = shift;
my $magic = " <font size=-1>look ma, no submit!</font><br>";
# see if input is defined
if ( not defined $input ) {
return("input not defined or NaN" . $magic);
}
# see if value is a number (*thanks Randall!*)
if ( $input !~ /\A\d+\z/ ) {
return("input is NaN" . $magic);
}
# got a number, so mod by 2
$input % 2 == 0 ? return("$input is EVEN" . $magic) : return("$input is ODD" . $magic);
}; # don't forget the trailing ';', since this is an anon subroutine
my $pjx = new CGI::Ajax( 'evenodd' => $evenodd_func );
$pjx->JSDEBUG(2);
scripts/pjx_manyret.pl view on Meta::CPAN
my $q = new CGI;
my $exported_fx = sub {
my $value_a = shift;
my $value_b = shift;
$value_a = "" if not defined $value_a; # make sure there's def
$value_b = "" if not defined $value_b; # make sure there's def
if ( $value_a =~ /\D+/ or $value_a eq "" ) {
return( $value_a, $value_b, 'NaN' );
} elsif ( $value_b =~ /\D+/ or $value_b eq "" ) {
return( $value_a, $value_b, 'NaN' );
} else {
# got two numbers, so lets multiply them together
return( $value_a, $value_b, $value_a * $value_b );
}
};
my $Show_Form = sub {
my $html = "";
$html .= <<EOT;
scripts/pjx_podex.pl view on Meta::CPAN
# define an anonymous perl subroutine that you want available to
# javascript on the generated web page.
my $evenodd_func = sub {
my $input = shift;
my $magic = " <font size=-1>look ma, no submit!</font><br>";
# see if input is defined
if ( not defined $input ) {
return("input not defined or NaN" . $magic);
}
# see if value is a number (*thanks Randall!*)
if ( $input !~ /\A\d+\z/ ) {
return("input is NaN" . $magic);
}
# got a number, so mod by 2
$input % 2 == 0 ? return("$input is EVEN" . $magic) : return("$input is ODD" . $magic);
}; # don't forget the trailing ';', since this is an anon subroutine
# define a function to generate the web page - this can be done
# million different ways, and can also be defined as an anonymous sub.
# The only requirement is that the sub send back the html of the page.
( run in 0.265 second using v1.01-cache-2.11-cpan-4d50c553e7e )