CGI::Lazy::ID - CGI::Lazy::ID documentation


CGI-Lazy documentation Contained in the CGI-Lazy distribution.

Index


Code Index:

LEGAL

Top

NAME

Top

CGI::Lazy::ID

SYNOPSIS

Top

	my $id = $session->id->generate();

DESCRIPTION

Top

Module to generate unique id's. Inspiration and ideas for this module, and even code itself was taken from PlainBlack's WebGUI with many thanks and much appreciation. WebGUI was awesome, but far heavier a tool than what was needed.

METHODS

Top

generate ( )

Generates a unique identifier.

session ( )

Returns the session object this object was created with.

new ( session )

Constructor.

session

CGI::Lazy::Session object

valid ($sessionID)

Returns true if $sessionID is a valid id string


CGI-Lazy documentation Contained in the CGI-Lazy distribution.

package CGI::Lazy::ID;

use strict;

use Digest::MD5;
use Time::HiRes qw(gettimeofday);
use CGI::Lazy::Globals;

#---------------------------------------------------------------------------------------
sub generate {
	my $self = shift;
	
	my($s,$us)=gettimeofday();
  	my($v)=sprintf("%09d%06d%10d%06d%255s",rand(999999999),$us,$s,$$,$self->session->config->configfile);
	my $id = Digest::MD5::md5_base64($v);
	$id =~ tr{+/}{_-};
	return $id;
}

#---------------------------------------------------------------------------------------
sub new	{
	my $class = shift;
	my $session = shift;

	srand;

	bless {_session => $session}, $class;
}

#---------------------------------------------------------------------------------------
sub session {
	my $self = shift;
	return $self->{_session};
}

#---------------------------------------------------------------------------------------
sub valid {
	my $self = shift;
	my $sessionID = shift || '';

	return $sessionID =~ m/^[\w\d-]{22}$/;
}

1

__END__