Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 4fc07be4

Von Sven Schöling vor mehr als 12 Jahren hinzugefügt

  • ID 4fc07be473b8e80e5c808d2876762383f660aa25
  • Vorgänger 4ab897fd
  • Nachfolger d686db65

SL::SessionFile::Random - damit man sich nicht selber einen Namen ausdenken muss

Unterschiede anzeigen:

SL/SessionFile.pm
24 24
  $file_name =~ s:.*/::g;
25 25
  $file_name =  "${path}/${file_name}";
26 26

  
27
  $self->file_name($file_name);
28

  
27 29
  if ($params{mode}) {
28 30
    my $mode = $params{mode};
29 31

  
......
35 37
    $self->fh(IO::File->new($file_name, $mode));
36 38
  }
37 39

  
38
  $self->file_name($file_name);
39

  
40 40
  return $self;
41 41
}
42 42

  
43
sub open {
44
  my ($self, $mode) = @_;
45
  return $self->fh(IO::File->new($self->file_name, $mode));
46
}
47

  
43 48
sub exists {
44 49
  my ($self) = @_;
45 50
  return -f $self->file_name;
......
139 144
it has been created for "customer.csv" then the value returned might
140 145
be C<users/session_files/e8789b98721347/customer.csv>.
141 146

  
147
=item C<open, %params]>
148

  
149
Opens the file_name given at creation with the given parameters.
150

  
142 151
=item C<exists>
143 152

  
144 153
Returns trueish if the file exists.
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

Auch abrufbar als: Unified diff