kivitendo/SL/SessionFile/Random.pm @ 7b1da9c3
4fc07be4 | Sven Schöling | package SL::SessionFile::Random;
|
||
use strict;
|
||||
use parent qw(SL::SessionFile);
|
||||
my @CHARS = ('A'..'Z', 'a'..'z', 0..9, '_');
|
||||
my $template = 'X' x 10;
|
||||
use constant MAX_TRIES => 1000;
|
||||
sub new {
|
||||
my ($class, %params) = @_;
|
||||
my $filename;
|
||||
my $tries = 0;
|
||||
$filename = _get_file() while $tries++ < MAX_TRIES && (!$filename || -e $filename);
|
||||
$class->SUPER::new($filename, %params);
|
||||
}
|
||||
sub _get_file {
|
||||
my $filename = $template;
|
||||
$filename =~ s/X(?=X*\z)/$CHARS[ int( rand( @CHARS ) ) ]/ge;
|
||||
$filename;
|
||||
}
|
||||
1;
|
||||
__END__
|
||||
=encoding utf-8
|
||||
=head1 NAME
|
||||
SL::SessionFile::Random - SessionFile with a random name
|
||||
=head1 SYNOPSIS
|
||||
use SL::SessionFile::Random;
|
||||
70e38287 | Bernd Bleßmann | # Create a session file with a unique random name
|
||
my $sfile = SL::SessionFile::Random->new(mode => "w");
|
||||
4fc07be4 | Sven Schöling | $sfile->fh->print("col1;col2;col3\n" .
|
||
"value1;value2;value3\n");
|
||||
$sfile->fh->close;
|
||||
=head1 DESCRIPTION
|
||||
70e38287 | Bernd Bleßmann | This modules gives you a random file in the current session cache that is
|
||
guaranteed to be unique.
|
||||
4fc07be4 | Sven Schöling | |||
=head1 FUNCTIONS
|
||||
70e38287 | Bernd Bleßmann | same as SL::SessionFile
|
||
4fc07be4 | Sven Schöling | |||
=head1 BUGS
|
||||
NONE yet.
|
||||
=head1 AUTHOR
|
||||
Sven Schoeling E<lt>s.schoeling@linet-services.deE<gt>
|
||||
=cut
|