Print Archive::Zip zip file to Apache2::RequestIO object - perl

I have a website using mod_perl.
I'm creating a zip file in memory (using Archive::Zip), and I want to serve that file without having to write it to disk.
Archive::Zip will only output to a specified file handle, and I don't think Apache2::RequestIO provides me with one.
At the moment I just print the Zip file to *STDOUT and that works. But I'm sure there's a better way to do it. I'm printing everything else through the RequestRec object, e.g. $r->print(...)

Something like this should help...
use Archive::Zip;
my $zip = Archive::Zip->new();
#create your zip here
use IO::Scalar;
my $memory_file = ''; #scalar as a file
my $memfile_fh = IO::Scalar->new(\$memory_file); #filehandle to the scalar
# write to the scalar $memory_file
my $status = $zip->writeToFileHandle($memfile_fh);
$memfile_fh->close;
#print with apache
#$r->content_type(".......");
$r->print($memory_file); #the content of a file-in-a-scalar
EDIT:
The above is obsoloted.
from the Archive::Zip docs:
Try to avoid IO::Scalar
One of the most common ways to use Archive::Zip is to generate Zip
files in-memory. Most people have use IO::Scalar for this purpose.
Unfortunately, as of 1.11 this module no longer works with IO::Scalar
as it incorrectly implements seeking.
Anybody using IO::Scalar should consider porting to IO::String, which
is smaller, lighter, and is implemented to be perfectly compatible
with regular seekable filehandles.
Support for IO::Scalar most likely will not be restored in the future,
as IO::Scalar itself cannot change the way it is implemented due to
back-compatibility issues.

In versions of Perl 5.8+, it seems like you can skip IO::Scalar and IO::String all together.
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
my $zip = Archive::Zip->new();
my $memory_file = ''; #scalar as a file
open(my $fh, '>', \$memory_file) || die "Couldn't open memory file: $!";
my $status = $zip->writeToFileHandle($fh);
$fh->close;
$r->print($memory_file);
I think there is probably a more optimal way of doing this, but it works...

Related

Perl: Open a file from a URL

I wanted to know how to open a file from a URL rather than a local file and I found the following answer on another thread:
use IO::String;
my $handle = IO::String->new(get("google.com"));
my #lines = <$handle>;
close $handle;
This works perfectly... on my PC...
But when I transferred the code over to my hosted server it complains that it can't find the IO module. So is there another way to open a file from an URL, that doesn't require any external modules (or uses one that is pretty much installed on every server)...?
You can install PerlIO::http, which will give you an input layer for opening a filehandle from a URL via open. This thing is not included in the Perl core, but it will work with Perls as early as 5.8.9.
Once you've installed it, all you need to do is open with a layer :http in the mode argument. There is nothing to use here. That happens automatically.
open my $fh, '<:http', 'https://metacpan.org/recent';
You can then read from $fh like a regular file. Under the hood it will take care of getting the data over the wire.
while (my $line = <$fh>) { ... }
There is no way to "open a file from a URL" as you ask. Well, I suppose you could throw something together using the progress() callback from LWP::UserAgent, but even then I don't think it would work how you want it to.
But you can make something that looks like it's doing what you want pretty easily. Actually, what we're really doing is pulling all the data back from the URL and then opening a filehandle on a string that contains that data.
use LWP::Simple;
my $data = get('https://google.com');
open my $url_fh, '<', \$data or die $!;
# Now $url_fh is a filehandle wrapped around your data.
# Treat it like any other filehandle.
while (<$url_fh>) {
print;
}
Your problem was that IO::String wasn't installed. But there's no need to install it, as it's simple enough to do what it does with standard Perl features (simply open a filehandle on a reference to a string).
Update: IO::String is completely unnecessary here. Not only because you can do what it does very simply, by just opening a filehandle on a reference to your string, but also because all you want to do is to read a file from a web site into an array. And in that case, your code is simply:
use LWP::Simple;
my $url = 'something';
my #records = split /\n/, get($url);
You might even consider adding some error handing.
use LWP::Simple;
my $url = 'something';
my $data = get($url);
die "No data found\n" unless defined $data;
my #array = split /\n/, get($url);

perl: canot open file within a loop

I am trying to read in a bunch of similar files and process them one by one. Here is the code I have. But somehow the perl script doesn't read in the files correctly. I'm not sure how to fix it. The files are definitely readable and writable by me.
#!/usr/bin/perl
use strict;
use warnings;
my #olap_f = `ls /full_dir_to_file/*txt`;
foreach my $file (#olap_f){
my %traits_h;
open(IN,'<',$file) || die "cannot open $file";
while(<IN>){
chomp;
my #array = split /\t/;
my $trait = $array[4];
$traits_h{$trait} ++;
}
close IN;
}
When I run it, the error message (something like below) showed up:
cannot open /full_dir_to_file/a.txt
You have newlines at the end of each filename:
my #olap_f = `ls ~dir_to_file/*txt`;
chomp #olap_f; # Remove newlines
Better yet, use glob to avoid launching a new process (and having to trim newlines):
my #olap_f = glob "~dir_to_file/*txt";
Also, use $! to find out why a file couldn't be opened:
open(IN,'<',$file) || die "cannot open $file: $!";
This would have told you
cannot open /full_dir_to_file/a.txt
: No such file or directory
which might have made you recognize the unwanted newline.
I'll add a quick plug for IO::All here. It's important to know what's going on under the hood but it's convenient sometimes to be able to do:
use IO::All;
my #olap_f = io->dir('/full_dir_to_file/')->glob('*txt');
In this case it's not shorter than #cjm's use of glob but IO::All does have a few other convenient methods for working with files as well.

Reading XLS file using Perl

This is my Perl script
#!/usr/bin/perl -w
use strict;
use warnings;
use Spreadsheet::WriteExcel;
use Spreadsheet::ParseExcel;
my $workbook = Spreadsheet::ParseExcel->new("/home/Admin/Desktop/RP_processed_Address_withsubscriptionID (1).xls");
my $workbook1 = Spreadsheet::WriteExcel->new("/home/Admin/Desktop/new.xls");
open(my$old, '<', "$workbook") or die "oops!";
open(my$new, '>', "$workbook1") or die "ooops!";
while (my$line = <$workbook>) {
print $workbook1 $line
}
When I run this Script I'm getting following error
Odd number of elements in hash assignment at /usr/local/share/perl5/Spreadsheet/ParseExcel.pm line 167.
oops! at sample.pl line 9.
I'm not getting any idea where is script is going wrong . Please help me how to resolve this error
your suggestions will be appreciable.
You are not reading any docs again. You copy and paste code and don't understand the basics of what you do. Why are you opening files using open when you already open them using the two modules? Why do you write a line manually? This is not how excel data works, this is not how the modules work. Stop guessing. Learn what you're doing. This will never work.
Take a look at CPAN for Spreadsheet::ParseExcel
You need to access the worksheets within the workbook object you've created and determine which you would like to parse data from. From here you can access cells using the column/width coordinate system. You don't need to use 'open' as the ParseExcel and WriteExcel already do this.
my $sheet = $workbook1->worksheet('Sheet1');
my $cell = $sheet->get_cell( 0, 0 );
my $cell_value = $cell->value();
Is it a bit more clear on what you need to do?

How do I check if a Unicode directory exists on Windows in Perl?

I need to check whether a Unicode directory exists in Perl. I am using Windows XP and Perl Camelbox 5.10.0.
If I try to create a directory (like Sinan suggested here stackoverflow.com/questions/2184726) that already exists the program dies.
Unfortunately if ( !-d $dir_name ) { # create directory $dir_name } doesn't seem to recognize Unicode directories or I am doing something completely stupid. I tried to encode the directory name before checking it, but the result is the same.
How do I check for the existance of a Unicode directory?
When answering your earlier question, I forgot that Win32.pm provides a decent interface. I will go back and that answer. However, for your immediate problem, what you need to do is not to automatically die when the CreateDirectory call fails, but to check the error code. If the error code is 0xb7 (ERROR_ALREADY_EXISTS), you go on your merry way.
The problem is that it is hard to go on your merry way using Perl functions when you have a Unicode file name. The solution is to use Win32::GetANSIPath (just keep an eye on the full length of the path):
#!/usr/bin/perl
use strict; use warnings;
use utf8;
use Encode qw( encode );
use File::Slurp;
use File::Spec::Functions qw( catfile );
use Win32;
use Win32::API;
use constant ERROR_ALREADY_EXISTS => 0xb7;
my $dir_name = 'Волгогра́д';
unless ( Win32::CreateDirectory($dir_name) ) {
my $err = $^E;
if ( $err == ERROR_ALREADY_EXISTS ) {
warn "Directory exists, no problem\n";
}
else {
die Win32::FormatMessage($^E);
}
}
my $ansi_path = Win32::GetANSIPathName($dir_name);
warn "$ansi_path\n";
Oh, and, good luck deleting that directory.
In a serious vein, though, the whole Windows Unicode file operations thing is a bit of a mess.
As far as I understand these things, you need the ANSI path name if you want to be able to use Perl functions such as open to work with paths containing Unicode characters. E.g.:
my $file = catfile($dir_name, 'test.txt');
open my $fh, '>', $file
or die "cannot create '$file': $!";
will fail whereas
my $file = catfile($ansi_path, 'test.txt');
open my $fh, '>', $file
or die "cannot create '$file': $!";
will succeed (at least on my system). You do not need the ANSI paths if you are going to only use Win32 API functions to deal with files (and that might be easier in your case). There are a bunch of modules to help you with the latter on CPAN.

What's the best way to open and read a file in Perl?

Please note - I am not looking for the "right" way to open/read a file, or the way I should open/read a file every single time. I am just interested to find out what way most people use, and maybe learn a few new methods at the same time :)*
A very common block of code in my Perl programs is opening a file and reading or writing to it. I have seen so many ways of doing this, and my style on performing this task has changed over the years a few times. I'm just wondering what the best (if there is a best way) method is to do this?
I used to open a file like this:
my $input_file = "/path/to/my/file";
open INPUT_FILE, "<$input_file" || die "Can't open $input_file: $!\n";
But I think that has problems with error trapping.
Adding a parenthesis seems to fix the error trapping:
open (INPUT_FILE, "<$input_file") || die "Can't open $input_file: $!\n";
I know you can also assign a filehandle to a variable, so instead of using "INPUT_FILE" like I did above, I could have used $input_filehandle - is that way better?
For reading a file, if it is small, is there anything wrong with globbing, like this?
my #array = <INPUT_FILE>;
or
my $file_contents = join( "\n", <INPUT_FILE> );
or should you always loop through, like this:
my #array;
while (<INPUT_FILE>) {
push(#array, $_);
}
I know there are so many ways to accomplish things in perl, I'm just wondering if there are preferred/standard methods of opening and reading in a file?
There are no universal standards, but there are reasons to prefer one or another. My preferred form is this:
open( my $input_fh, "<", $input_file ) || die "Can't open $input_file: $!";
The reasons are:
You report errors immediately. (Replace "die" with "warn" if that's what you want.)
Your filehandle is now reference-counted, so once you're not using it it will be automatically closed. If you use the global name INPUT_FILEHANDLE, then you have to close the file manually or it will stay open until the program exits.
The read-mode indicator "<" is separated from the $input_file, increasing readability.
The following is great if the file is small and you know you want all lines:
my #lines = <$input_fh>;
You can even do this, if you need to process all lines as a single string:
my $text = join('', <$input_fh>);
For long files you will want to iterate over lines with while, or use read.
If you want the entire file as a single string, there's no need to iterate through it.
use strict;
use warnings;
use Carp;
use English qw( -no_match_vars );
my $data = q{};
{
local $RS = undef; # This makes it just read the whole thing,
my $fh;
croak "Can't open $input_file: $!\n" if not open $fh, '<', $input_file;
$data = <$fh>;
croak 'Some Error During Close :/ ' if not close $fh;
}
The above satisfies perlcritic --brutal, which is a good way to test for 'best practices' :). $input_file is still undefined here, but the rest is kosher.
Having to write 'or die' everywhere drives me nuts. My preferred way to open a file looks like this:
use autodie;
open(my $image_fh, '<', $filename);
While that's very little typing, there are a lot of important things to note which are going on:
We're using the autodie pragma, which means that all of Perl's built-ins will throw an exception if something goes wrong. It eliminates the need for writing or die ... in your code, it produces friendly, human-readable error messages, and has lexical scope. It's available from the CPAN.
We're using the three-argument version of open. It means that even if we have a funny filename containing characters such as <, > or |, Perl will still do the right thing. In my Perl Security tutorial at OSCON I showed a number of ways to get 2-argument open to misbehave. The notes for this tutorial are available for free download from Perl Training Australia.
We're using a scalar file handle. This means that we're not going to be coincidently closing someone else's file handle of the same name, which can happen if we use package file handles. It also means strict can spot typos, and that our file handle will be cleaned up automatically if it goes out of scope.
We're using a meaningful file handle. In this case it looks like we're going to write to an image.
The file handle ends with _fh. If we see us using it like a regular scalar, then we know that it's probably a mistake.
If your files are small enough that reading the whole thing into memory is feasible, use File::Slurp. It reads and writes full files with a very simple API, plus it does all the error checking so you don't have to.
There is no best way to open and read a file. It's the wrong question to ask. What's in the file? How much data do you need at any point? Do you need all of the data at once? What do you need to do with the data? You need to figure those out before you think about how you need to open and read the file.
Is anything that you are doing now causing you problems? If not, don't you have better problems to solve? :)
Most of your question is merely syntax, and that's all answered in the Perl documentation (especially (perlopentut). You might also like to pick up Learning Perl, which answers most of the problems you have in your question.
Good luck, :)
It's true that there are as many best ways to open a file in Perl as there are
$files_in_the_known_universe * $perl_programmers
...but it's still interesting to see who usually does it which way. My preferred form of slurping (reading the whole file at once) is:
use strict;
use warnings;
use IO::File;
my $file = shift #ARGV or die "what file?";
my $fh = IO::File->new( $file, '<' ) or die "$file: $!";
my $data = do { local $/; <$fh> };
$fh->close();
# If you didn't just run out of memory, you have:
printf "%d characters (possibly bytes)\n", length($data);
And when going line-by-line:
my $fh = IO::File->new( $file, '<' ) or die "$file: $!";
while ( my $line = <$fh> ) {
print "Better than cat: $line";
}
$fh->close();
Caveat lector of course: these are just the approaches I've committed to muscle memory for everyday work, and they may be radically unsuited to the problem you're trying to solve.
I once used the
open (FILEIN, "<", $inputfile) or die "...";
my #FileContents = <FILEIN>;
close FILEIN;
boilerplate regularly. Nowadays, I use File::Slurp for small files that I want to hold completely in memory, and Tie::File for big files that I want to scalably address and/or files that I want to change in place.
For OO, I like:
use FileHandle;
...
my $handle = FileHandle->new( "< $file_to_read" );
croak( "Could not open '$file_to_read'" ) unless $handle;
...
my $line1 = <$handle>;
my $line2 = $handle->getline;
my #lines = $handle->getlines;
$handle->close;
Read the entire file $file into variable $text with a single line
$text = do {local(#ARGV, $/) = $file ; <>};
or as a function
$text = load_file($file);
sub load_file {local(#ARGV, $/) = #_; <>}
If these programs are just for your productivity, whatever works! Build in as much error handling as you think you need.
Reading in a whole file if it's large may not be the best way long-term to do things, so you may want to process lines as they come in rather than load them up in an array.
One tip I got from one of the chapters in The Pragmatic Programmer (Hunt & Thomas) is that you might want to have the script save a backup of the file for you before it goes to work slicing and dicing.
The || operator has higher precedence, so it is evaluated first before sending the result to "open"... In the code you've mentioned, use the "or" operator instead, and you wouldn't have that problem.
open INPUT_FILE, "<$input_file"
or die "Can't open $input_file: $!\n";
Damian Conway does it this way:
$data = readline!open(!((*{!$_},$/)=\$_)) for "filename";
But I don't recommend that to you.