Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Question about the upload function in CGI.pm

by technojosh (Priest)
on Dec 06, 2007 at 17:13 UTC ( #655462=perlquestion: print w/ replies, xml ) Need Help??
technojosh has asked for the wisdom of the Perl Monks concerning the following question:

I had previously posted Having an issue with my CGI.pm file uploads, in which my file uploads were showing up on the server as 0 bytes, but showing up nonetheless.

I was getting errors in my log about 'readline on unopened filehandle' and such...

Now, the code i had posted was part of a bigger site (probably goes without saying, but whatever). There are probably 6-7 other Perl apps in the cgi-bin, and all of them use CGI;. Most of them use an underlying module that houses a number of functions for the site.

Although I got lots of nice pointers and help in Having an issue with my CGI.pm file uploads, the problem ended up being that I have:

use CGI; my $i = new CGI;
at the top of both the upload page in question, and the supporting package. It wasn't even being used in the package, I probably had put it there previously for some other grand idea i had. So, when I removed the use CGI; from the package, the upload problem was fixed.

I suppose I'll get to my questions:

1. Why did this ONLY cause an issue with my file uploads (why had it never caused an issue with any of the other CGI.pm parameter handling between pages?)?

and most important to me...

2. What can I do to clean that up? Or if that isn't worded right, what can I do to use CGI; in my package so that all pages that also use CGI; won't run into this problem?

Comment on Question about the upload function in CGI.pm
Select or Download Code
Re: Question about the upload function in CGI.pm
by kyle (Abbot) on Dec 06, 2007 at 17:23 UTC

    In answer to your first question, your file uploads are almost certainly arriving via STDIN, and the first time you new CGI, it reads all of STDIN. The second time you new CGI, there's nothing left on STDIN for it to read, so it doesn't see the file contents.

    Besides STDIN, CGI also gets parameter information from %ENV (see perlvar), which doesn't change after being read.

    In answer to your second question, you basically have to make sure to create only one CGI object, which every module will use. A simple singleton package can accomplish this:

    package My::CGI; use CGI; my $cgi; sub cgi { $cgi ||= CGI->new(); return $cgi; } __END__ # use this way: use My::CGI; my $cgi = My::CGI->cgi();
Re: Question about the upload function in CGI.pm
by trwww (Priest) on Dec 06, 2007 at 22:14 UTC

    Hello,

    1. Why did this ONLY cause an issue with my file uploads (why had it never caused an issue with any of the other CGI.pm parameter handling between pages?)?

    See Re^3: Uploading Files with CGI.pm results in 0 byte files

    2. What can I do to clean that up? Or if that isn't worded right, what can I do to use CGI; in my package so that all pages that also use CGI; won't run into this problem?

    Use a tool that handles preparing your input data for you (like CGI::Application). Check out My contribution to #584968.

    Regards,

    trwww

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://655462]
Approved by ww
Front-paged by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2014-08-01 06:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (256 votes), past polls