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}?


In reply to Ok, what am I doing wrong with my use of CGI::Upload? by northwind

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.