X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FSessionFile.pm;h=6f04041e1a34bb56dd8cad7779d7fcfffb66a1f1;hb=cca939527d780d2a5c1c152043c789d11d290ad5;hp=b9a5b7697718d96712a5ec646fcea4185e441f6c;hpb=30e09cb2e85d2eab0d3cd9653eafdfef51df8645;p=kivitendo-erp.git diff --git a/SL/SessionFile.pm b/SL/SessionFile.pm index b9a5b7697..6f04041e1 100644 --- a/SL/SessionFile.pm +++ b/SL/SessionFile.pm @@ -16,7 +16,7 @@ use Rose::Object::MakeMethods::Generic ); sub new { - my ($class, $file_name, $mode) = @_; + my ($class, $file_name, %params) = @_; my $self = $class->SUPER::new; @@ -24,12 +24,27 @@ sub new { $file_name =~ s:.*/::g; $file_name = "${path}/${file_name}"; - $self->fh(IO::File->new($file_name, $mode)) if $mode; $self->file_name($file_name); + if ($params{mode}) { + my $mode = $params{mode}; + + if ($params{encoding}) { + $params{encoding} =~ s/[^a-z0-9\-]//gi; + $mode .= ':encoding(' . $params{encoding} . ')'; + } + + $self->fh(IO::File->new($file_name, $mode)); + } + return $self; } +sub open { + my ($self, $mode) = @_; + return $self->fh(IO::File->new($self->file_name, $mode)); +} + sub exists { my ($self) = @_; return -f $self->file_name; @@ -85,7 +100,7 @@ destroyed or expires use SL::SessionFile; # Create a session file named "customer.csv" (relative names only) - my $sfile = SL::SessionFile->new("customer.csv", "w"); + my $sfile = SL::SessionFile->new('customer.csv', mode => 'w'); $sfile->fh->print("col1;col2;col3\n" . "value1;value2;value3\n"); $sfile->fh->close; @@ -108,13 +123,16 @@ C. =over 4 -=item C +=item C Create a new instance. C<$file_name> is a relative file name (path components are stripped) to the session-specific temporary directory. -If C<$mode> is given then try to open the file as an instance of -C. C<$mode> is passed through to C. +If C<$params{mode}> is given then try to open the file as an instance +of C. C<${mode}> is passed through to C. + +If C<$params{encoding}> is given then the file is opened with the +appropriate encoding layer. =item C @@ -126,6 +144,10 @@ Returns the full relative file name associated with this instance. If it has been created for "customer.csv" then the value returned might be C. +=item C + +Opens the file_name given at creation with the given parameters. + =item C Returns trueish if the file exists.