NAME

CGI::Session::Flash - The great new CGI::Session::Flash!

SYNOPSIS

use CGI::Session;
use CGI::Session::Flash;

my $session = CGI::Session->new(...);
my $flash   = CGI::Session::Flash->new($session);

# Get and set the values
$flash->set(KEY => @VALUES);
my @values = $flash->get('KEY');

# Mark a key as something to keep for another request.
$flash->keep('KEY');

# Mark a key to be discarded at the end of the request.
$flash->discard('KEY');

# Checking for keys and if the flash is empty.
print "Flash is empty\n"     if ($flash->is_empty);
print "Flash contains key\n" if ($flash->has_key('NAME'));
print "Flash keys: ", join(", ", $flash->keys), "\n";

# Dump the contents of the flash for debugging purposes.
warn $flash->dump();

# Refresh the flash, this basically just wipes it and starts fresh.
$flash->reset();

DESCRIPTION

This module implements a Flash object. A flash is session data with a specific life cycle. When you put something into the flash it stays there until two cleanup calls have been made. What this generally means is that in a web application the data in the flash will stay until the end of the next request. This allows you to use it for storing messages that can be accessed after a redirect, but then are automatically cleaned up.

METHODS

Constructor

CLASS->new($session, %options)

Create a new flash object. The first parameter is a session object. This is used to initialize the flash.

Additional arguments specify options for the flash. The possible options are

auto_cleanup

Set to a true or false value depending on whether you want to enable automatic cleanup. The default is true.

If enabled a call to cleanup will be invoked when the object goes out of scope.

session_key

This is the name of the key to use when storing the flash data in the session. The default value is _flash. This actual flash data is stored in this key and the list of flash keys to keep is stored in a key with _keep appended.

Accessors

$flash->session

Returns the associated session object.

$flash->auto_cleanup

Returns true or false, depending on whether auto cleanup is enabled.

$flash->session_key

Return the session key.

Getting and Setting Data

$flash->get('KEY')

Retrieve the values from the flash for the specified key.

An undefined value is returned if the key does not exist.

If only a single piece of data is in the flash for the key then it is returned. Otherwise all of the data is returned. This method is context aware so if there are multiple pieces of data for the key then they will be returned as an arrayref in scalar context.

$flash->set('KEY' => @values)

Set the values in the flash. Values can be a single item or a list of items. Internally they are stored as an arrayref.

Keep in mind that since we are storing the data for the flash in the session that it is best to just store simple things and not complex objects.

$flash->now('KEY' => @values)

This method is very similar to set. It takes the same arguments and sets the data in the flash. The primary difference is that after the data is set in the flash, a call to discard is made for the key. This causes the data to removed on the next call to cleanup.

Contents and Keys

$flash->contents()

This returns a hashref that represents the contents of the flash. The keys are the keys in the flash and the values are an arrayref of the values.

This is not likely to be called directly, instead using get. This method is used internally when storing the flash data into the session.

$flash->keep_keys()

Returns a list of keys that are currently marked as being kept. If called in scalar context then it will return an arrayref.

$flash->keys()

Returns a list of keys that are in the flash.

$flash->has_key('KEY')

Returns true or false depending on if the flash contains the specified key.

$flash->is_empty()

Returns true or false depending on if the flash is empty.

$flash->reset()

Reset the flash. This wipes all data and kept keys.

Cleanup

When an object goes out of scope the cleanup and flush methods are automatically called.

$flash->keep(@keys)

Mark the specified keys as being kept. If no keys are specified then all keys currently in the flash will be kept. See cleanup for details on how this works.

$flash->discard(@keys)

Mark the specified keys as being discarded. If no keys are specified then all keys currently in the flash will be marked as being discarded. Once marked as discarded they will be removed the next time that cleanup is called.

$flash->cleanup()

Perform cleanup of the flash.

This method goes through all of the keys in the flash. If the key is not marked as being kept it is removed. If it is marked as being kept then it is not removed, but it is instead marked as being discarded for the next time that the method is called.

$flash->flush()

Save the contents of the flash back into the session.

Debugging

$flash->dump()

This method returns a Data::Dumper representation of the contents of the flash. This is useful for debugging purposes.

BUGS

Please report any bugs or feature requests to bug-cgi-session-flash at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CGI-Session-Flash. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

ACKNOWLEDGEMENTS

The concept and name of this module was inspired by the Ruby on Rails framework.

SEE ALSO

CGI::Session

AUTHOR

Bradley C Bailey, <cgi-session-flash at brad.memoryleak.org>

COPYRIGHT & LICENSE

Copyright 2008 Bradley C Bailey, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.