ASP4
view release on metacpan or search on metacpan
README.markdown view on Meta::CPAN
### $Request->FileUpload( $field_name )
if( my $file = $Request->FileUpload('avatar_pic') ) {
# Handle the uploaded file:
$file->SaveAs( "/var/media/$Session->{user_id}/avatar/" . $file->FileName );
}
See also the [ASP4::FileUpload](http://search.cpan.org/perldoc?ASP4::FileUpload) documentation.
## $Response
An instance of [ASP4::Response](http://search.cpan.org/perldoc?ASP4::Response), the `$Response` object gives shortcuts for dealing
with the outgoing reply from the server back to the client.
Examples:
### $Response->Write( $string )
The following example prints the string `Hello, World!` to the browser:
$Response->Write("Hello, World!");
Or, within an ASP script, `<%= "Hello, World" %>`
### $Response->Redirect( $url )
$Response->Redirect( "/new/url/?foo=bar" );
### $Response->SetCookie( %args )
Setting cookies works as follows:
$Response->SetCookie(
name => "cookie-name",
value => "the-value",
# The rest of these arguments are optional:
# Expires: (If you don't specify the "expires" argument, the cookie will
# be deleted when the browser is closed.
expires => "3D", # 3 days
expires => "3H", # or 3 hours
expires => "3M", # or 3 minutes
# Domain: (defaults to $ENV{HTTP_HOST})
domain => ".example.com", # works for *.example.com
domain => "www.example.com", # will ONLY work for www.example.com
# Path:
path => "/some/folder/" # will ONLY work within /some/folder/ on your website
);
### $Response->Include( $path, %args )
ASP4's `$Response` object offers 3 different include methods.
<!-- Normal SSI-style Include -->
<!-- #include virtual="/includes/page.asp" -->
If you want to supply arguments to the included ASP script you can use `$Response->Include($path, \%args)`
# Add the output of C</includes/page.asp> to the current output buffer:
my %args = ( foo => "bar" );
$Response->Include( $Server->MapPath("/includes/page.asp"), \%args );
`\%args` is optional.
Within the included ASP script, `\%args` is accessible like this:
<%
my ($self, $context, $args) = @_;
%>
### $Response->TrapInclude( $path, %args )
Or if you need to capture the result of executing an ASP script and use it within
a variable, use `$Response->TrapInclude($path, \%args)`
# Capture the output of C</includes/page.asp>:
my %args = ( foo => "bar" );
my $html = $Response->TrapInclude( $Server->MapPath("/includes/page.asp"), \%args );
`\%args` is optional.
Within the included ASP script, `\%args` is accessible like this:
<%
my ($self, $context, $args) = @_;
%>
## $Session
The `$Session` object is an instance of a subclass of [ASP4::SessionStateManager](http://search.cpan.org/perldoc?ASP4::SessionStateManager)
(depending on your website's configuration).
The `$Session` object is a simple blessed hashref and should be used like a hashref.
Examples:
### Set a session variable
$Session->{foo} = "bar";
$Session->{thing} = {
banana => "yellow",
cherry => "red",
peach => "pink,
};
### Get a session variable
my $foo = $Session->{foo};
### $Session->save()
Called automatically at the end of every successful request, causes any changes
to the `$Session` to be saved to the database.
README.markdown view on Meta::CPAN
# password2:
if( length($Form->{password2}) ) {
if( length($Form->{password}) ) {
unless( $Form->{password} eq $Form->{password2} ) {
$errors->{password2} = "Passwords don't match";
}
}
}
else {
$errors->{password2} = "Required";
}
# Bail out of we already have errors:
return $errors if keys %$errors;
# See if the user already exists:
if( App::db::user->count_search( email => $Form->{email} ) ) {
$errors->{email} = "Already in use";
}
# Errors or not?:
keys %$errors ? return $errors : return;
}
1;# return true:
File: `htdocs/profile.asp`
<%@ Page UseMasterPage="/masters/global.asp" %>
<asp:Content PlaceHolderID="meta_title">My Profile</asp:Content>
<asp:Content PlaceHolderID="headline">My Profile</asp:Content>
<asp:Content PlaceHolderID="main_content">
<%
if( my $msg = $Session->{msg} ) {
%>
<div class="message"><%= $msg %></div>
<%
}# end if()
%>
<%
# Get our $user:
use App::db::user;
my $user = App::db::user->retrieve( $Session->{user_id} );
%>
<div style="float: left; width: 40%; border-right: solid 1px #000;">
<h3>Incoming Messages</h3>
<%
foreach my $msg ( $user->messages_in(undef, { order_by => "created_on ASC"} ) ) {
%>
<div class="msg">
<span class="from"><%= $msg->sender->email %></span> says:<br/>
<div class="body"><%= $Server->HTMLEncode( $msg->body ) %></div>
<span class="date"><%= $msg->created_on %></span>
</div>
<%
}# end foreach()
%>
</div>
<div style="float: right; width: 40%; border: dotted 1px #000;">
<h3>Send New Message</h3>
<form id="send_form" method="post" action="/handlers/app.send">
<p>
<label>Recipient:</label>
<select name="to_user_id">
<%
my @users = App::db::user->search_where({
user_id => {'!=' => $user->id }
}, {
order_by => "email"
});
foreach my $user ( @users ) {
%>
<option value="<%= $user->id %>"><%= $Server->HTMLEncode( $user->email ) %></option>
<%
}# end foreach()
%>
</select>
</p>
<p>
<label>Subject:</label>
<input type="text" name="subject" maxlength="100" />
</p>
<p>
<label>Message:</label><br/>
<textarea name="body"></textarea>
</p>
<p>
<input type="submit" value="Send Message" />
</p>
</form>
</div>
</asp:Content>
The form submits to `/handlers/app.send` which maps to `handlers/app/send.pm`
File: `handlers/app/send.pm`
package app::send;
use strict;
use warnings 'all';
use base 'ASP4::FormHandler';
use vars __PACKAGE__->VARS;
use App::db::user;
use App::db::message;
sub run {
my ($self, $context) = @_;
# Create the message:
my $msg = eval {
App::db::message->do_transaction(sub {
my $msg = App::db::message->create(
from_user_id => $Session->{user_id},
to_user_id => $Form->{to_user_id},
subject => $Form->{subject},
( run in 2.831 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )