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:

Error executing run mode 'import_file': $VAR1 = { 'handle' => bless( \*IO::File::_GEN_0, 'IO::File' ), 'name' => 'test_file.txt', 'type' => 'txt', 'mime' => 'text/plain' };
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>

Trying

my @foo = (); while($_ = $file{handle}->getline) { push @foo, $_; }
(because IO::File inherits from IO::Handle, which is where we find ->getline) gives us nothing pushed into @foo.  Ok, fine.  Trying
my @foo = (); while($_ = \*{$file{handle}}->getline) { push @foo, $_; }
gives the error
Error executing run mode 'import_file': Can't call method "getline" wi +thout a package or object reference
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!", running
die "Weeee!!!!!" if($file{handle}->opened);
results in this being returned
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

my $buffer; my @results; my $fh = $upload->file_handle('upload'); while (read($fh, $buffer, 45)) { push (@results, pack("u", $buffer)); } $fh->close;
Merging the above example code into my code results in...  nothing!  Absolutely, positively, nothing is in @foo no matter what method is used!

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
    Where do you use $query (my $query = $self->query)? Is $self compatible with CGI::Upload which states it needs "any kind of CGI.pm like module. The requirements are that it has to support the ->param method and the ->upload method returning a file handle."?

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

      $query is a hold-over from the real code (I was trying to pare down the code into an example I could easily post).  $self is part of a CGI::Application framework.

        CGI::Upload needs the query object, not the CGI::App object. Try my $upload = CGI::Upload->new( { query => $query } );.
Re: Ok, what am I doing wrong with my use of CGI::Upload?
by Anonymous Monk on Feb 07, 2006 at 08:25 UTC
    What is in %INC at the time you're trying this (report anything with CGI in the name)?

    What server are you using?

    Did CGI::Upload pass all tests before you installed it?

    Divorce your code from CGI::Application, and see if you can get it to work standalone (and if that doesn't work, post that code here).

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.