ShopOrder: billing_email zusätzlich als invoice_mail ...
[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 with a unique random name
41   my $sfile = SL::SessionFile::Random->new(mode => "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
49 guaranteed to be unique.
50
51 =head1 FUNCTIONS
52
53 same as SL::SessionFile
54
55 =head1 BUGS
56
57 NONE yet.
58
59 =head1 AUTHOR
60
61 Sven Schoeling E<lt>s.schoeling@linet-services.deE<gt>
62
63 =cut