northwind has asked for the wisdom of the Perl Monks concerning the following question:
Ok, esteemed monks, I have a problem that has been racking my brains for far too long now. I am using CGI::Upload to do file uploading in a CGI::Application framework (changing the framework is beyond my control, please keep this in mind in your responses). My code is:
sub import_file { my $self = shift; my $query = $self->query(); my upload = CGI::Uload->new($self); my %file = ( name => $upload->file_name("upload"), type => $upload->file_type("upload"), handle => $upload->file_handle("upload"), mime => $upload->mime_type("upload"), );
Using Data::Dumper on %file gives this output:
Ok, all very well and good. So to get to the data in the uploaded file, we simply use IO::File methods on $file{handle}. Right? <SNL skit mode>WRONG!!!</SNL skit mode>Error executing run mode 'import_file': $VAR1 = { 'handle' => bless( \*IO::File::_GEN_0, 'IO::File' ), 'name' => 'test_file.txt', 'type' => 'txt', 'mime' => 'text/plain' };
Trying
(because IO::File inherits from IO::Handle, which is where we find ->getline) gives us nothing pushed into @foo. Ok, fine. Tryingmy @foo = (); while($_ = $file{handle}->getline) { push @foo, $_; }
gives the errormy @foo = (); while($_ = \*{$file{handle}}->getline) { push @foo, $_; }
Alright, we had it right the first time. Then why is nothing getting pushed into @foo? And before jumping to the conclusion of "Open the file, silly!", runningError executing run mode 'import_file': Can't call method "getline" wi +thout a package or object reference
results in this being returneddie "Weeee!!!!!" if($file{handle}->opened);
Error executing run mode 'import_file': Weeee!!!!!
Doing a Super Search resulted in finding CGI::Upload - CGI class for handling browser file uploads, by the original author of the module. In the aforementioned link, I found this example code
Merging the above example code into my code results in... nothing! Absolutely, positively, nothing is in @foo no matter what method is used!my $buffer; my @results; my $fh = $upload->file_handle('upload'); while (read($fh, $buffer, 45)) { push (@results, pack("u", $buffer)); } $fh->close;
So now I ask you, esteemed monks, how do I get at the data behind the filehandle held in $file{handle}?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Ok, what am I doing wrong with my use of CGI::Upload?
by japhy (Canon) on Feb 06, 2006 at 18:00 UTC | |
by northwind (Hermit) on Feb 06, 2006 at 18:05 UTC | |
by friedo (Prior) on Feb 06, 2006 at 18:15 UTC | |
by northwind (Hermit) on Feb 06, 2006 at 18:29 UTC | |
Re: Ok, what am I doing wrong with my use of CGI::Upload?
by Anonymous Monk on Feb 07, 2006 at 08:25 UTC |