Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

CGI Upload with OLD

by amelinda (Friar)
on Oct 17, 2001 at 22:16 UTC ( #119492=perlquestion: print w/replies, xml ) Need Help??

amelinda has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to write a CGI that accepts a file upload. I'm about to go totally nuts. Some helpful help would be appreciated.

Why am I going nuts, you ask? Why not use's handy upload() feature? Simple. That feature doesn't start until 2.47. I get 2.36. Before anyone says "install the new version" or "get them to install the new version" or even "install the new version in your directory": I can't. I've already asked the sysadmin to install it, but can't wait that long. I only have FTP access, so I'm not sure how to install through FTP. Thus, my request for some helpful help.

So, this is the code I have.
#!/usr/bin/perl -w use strict; use CGI qw(:standard); my $query = new CGI; print header; print start_html; my $user = $query->param('user'); my $password = $query->param('password'); { no strict; $filename = $query->param('foofile'); undef $/; $buffer = <$filename>; $buffer =~ tr/\r\n/\n/; @buffer= split /\n/, $buffer; } print $buffer[0]; print end_html;
This is the minim case, and it fails. Why? I have no idea. Apparently, I don't get CGI::Carp, either.

The undef $/; and tr bits are there for nasty file conversion issues.

Basically what seems to be happening is that @buffer is not global, so I can't see it outside that no strict; block. So I tried to put in, before that block, a my @buffer; line. That fails too.

I thought about trying to wade through and cut-n-pasting some code, but I'm kind of in a time crunch, and this almost works. :sigh:

Replies are listed 'Best First'.
(tye)Re: CGI Upload with OLD
by tye (Sage) on Oct 17, 2001 at 22:44 UTC

    Install the new version.


    Seriously, just install a new version of CGI on your local computer then FTP the resulting and CGI/*.pm files to your account and use lib '/full/path'; to your script where you now have /full/path/ and /full/path/CGI/*.pm in place. (Note that not all modules are this easy to fake an install for, though.)

            - tye (but my friends call me "Tye")
      Really? It was exactly because I didn't think it would be easy to fake an install for it that I didn't try to do that. Maybe I'll do that.


        It's not just easy, it's easy peasy pudding and pie - I do it all the time and I don't know ***t from shinola when it comes to managing unix systems.

        Tip for PC prisoners:
        To find the file path, run a script with an obvious, catastrophic error, then look up the error_log, and it'll give you the filepath to your script. Replace the script name with "lib" (and create a directory called "lib") and then you've got the path to go in "use lib '/path/path/pathedy/path/'

        This may sound absurd, but believe me, for this bear of little brain it was a major lifestyle improvement when I figured it out.

        George Sherston
Re: CGI Upload with OLD
by chromatic (Archbishop) on Oct 17, 2001 at 22:47 UTC
    It's not clear what "almost works" means. Are you expecting a filename or a filehandle from the param() call? (My guess is filename, based on fuzzy recollection). I'm also guessing that you're getting glob operation instead of a filehandle read, but that's a big shot in the dark.

    If declaring @buffer as a lexical before the block fails, either your installation of Perl is completely broken, or the code inside the block is completely broken. I'd lean towards the latter.

    Print the contents of $buffer before you transliterate or split, and see if it's what you expect. My guess is that it isn't.

      I should get a filehandle from the param() call. I do. I can read it all in. If I print the contents of $buffer, they are exactly as I expect. If I print elements of @buffer after the split, they are as I expect. As I mentioned, if my prints are all inside the no strict; block, they function as expected. It's just as if the data doesn't exist outside the block (which, I think, it doesn't).

      It "almost works" as in, if i stopped use strict;ing, it would probably all work.

Re: CGI Upload with OLD
by amelinda (Friar) on Oct 18, 2001 at 03:39 UTC
    I made it work.

    I cheated like a big dog, but it works. :smirk:

    I didn't mention that this file is uploading some data that we'll be searching later with an HTML form, did I? Well, in that case, it ought to be persistent... saved somewhere, right?

    So. Inside the no strict; block, I open a file and save off the data (after the tr and split). Once safely back outside that block, I open the datafile and read back the data and proceed merrily on my way. But it works!

Re: CGI Upload with OLD
by amelinda (Friar) on Oct 17, 2001 at 22:23 UTC
    Yes, the HTML form has the correct enctype, multipart/form-data. If I move that print $buffer[0] line inside the no strict; block, it works fine. But I actually want to do a whole LOT of stuff with the data, and I'd really rather not move it all inside the no strict; block. :)
(tye)Re2: CGI Upload with OLD
by tye (Sage) on Oct 18, 2001 at 18:59 UTC

    I don't quite understand the problem but I can recommend this solution (especially if I allow for some cut'n'paste errors):

    my $user = $query->param('user'); my $password = $query->param('password'); my @buffer; # !!! Added this line. { no strict; $filename = $query->param('foofile'); undef $/; my $buffer = <$filename>; # !!! Added "my" here. $buffer =~ tr/\r\n/\n/; @buffer= split /\n/, $buffer; } print $buffer[0];
    Without a change like that, the print $buffer[0]; line should have generated a fatal error.

    Update: amelinda was kind enough to /msg me that this has already been tried. That seems very odd so I'll resort to voodoo programming (if I were working on this myself I'd instead be debugging to check what 0+\@buffer and 0+\@main::buffer evaluate to both inside and outside that block to try to figure out what bizarre thing was going on):

    my $user = $query->param('user'); my $password = $query->param('password'); my @buffer= do { local( $/ ); #= undef no strict; my $filename = $query->param('foofile'); split /\r?\n/, <$filename>; } print $buffer[0];

            - tye (but my friends call me "Tye")

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://119492]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2020-10-29 01:10 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (265 votes). Check out past polls.