SL::SessionFile::Random - damit man sich nicht selber einen Namen ausdenken muss
[kivitendo-erp.git] / SL / SessionFile / Random.pm
1 package SL::SessionFile::Random;
2
3 use strict;
4 use parent qw(SL::SessionFile);
5
6 my @CHARS = ('A'..'Z', 'a'..'z', 0..9, '_');
7 my $template = 'X' x 10;
8 use constant MAX_TRIES => 1000;
9
10 sub new {
11   my ($class, %params) = @_;
12
13   my $filename;
14   my $tries = 0;
15   $filename = _get_file() while $tries++ < MAX_TRIES && (!$filename || -e $filename);
16
17   $class->SUPER::new($filename, %params);
18 }
19
20 sub _get_file {
21   my $filename = $template;
22   $filename =~ s/X(?=X*\z)/$CHARS[ int( rand( @CHARS ) ) ]/ge;
23   $filename;
24 }
25
26 1;
27
28 __END__
29
30 =encoding utf-8
31
32 =head1 NAME
33
34 SL::SessionFile::Random - SessionFile with a random name
35
36 =head1 SYNOPSIS
37
38   use SL::SessionFile::Random;
39
40   # Create a session file named "customer.csv" (relative names only)
41   my $sfile = SL::SessionFile::Random->new("w");
42   $sfile->fh->print("col1;col2;col3\n" .
43                     "value1;value2;value3\n");
44   $sfile->fh->close;
45
46 =head1 DESCRIPTION
47
48 This modules gives you a random file in the current session cache that is guaranteed to be unique
49
50 =head1 FUNCTIONS
51
52 same as SL::SessioNFile
53
54 =head1 BUGS
55
56 NONE yet.
57
58 =head1 AUTHOR
59
60 Sven Schoeling E<lt>s.schoeling@linet-services.deE<gt>
61
62 =cut