http://www.perlmonks.org?node_id=295339

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

Hello all,

I got roped into coding some stuff for a IIs box, bleah. My normal file upload routine is getting the error

Undefined subroutine CGI::upload

The eversion for CGI is 2.46 but from reading the revision history it does not appear that should be the issue. I can't get the folks running the box to answer any questions so far.

Anyone have any advice? Code is below.
TIA
jg

#!/usr/local/bin/perl -w use strict; use CGI::Carp qw/fatalsToBrowser /; use CGI qw/:standard /; my $q = CGI->new(); my $user_pass = $q->param('pass'); my $target_directory = "c:\\rediculousIIsServer\\yata"; if ($user_pass eq 'somePass') { file_up(); } else { print $q->header, start_html, h2('Bad Password=', $user_pass); exit; } sub file_up { my $corrected_filename = $q->param('temp_filename'); $corrected_filename =~ s/ /_/g; local $| = 1; my ($bytesread,$buffer,$file); my $fh = $q->upload('temp_filename'); open(OUTF, '>' . $target_directory . $corrected_filename); while ($bytesread = read($fh, $buffer, 1024)) { print(OUTF $buffer); } close(OUTF); if (!$file && $q->cgi_error) { print($q->header(-status=>$q->cgi_error)); exit 0; } my $redir = "http://www.thesite.com/done.htm"; print $q->redirect("$redir"); }
_____________________________________________________
"The man who grasps principles can successfully select his own methods.
The man who tries methods, ignoring principles, is sure to have trouble.
~ Ralph Waldo Emerson

Replies are listed 'Best First'.
Re: Undefined subroutine CGI::upload
by matthewb (Curate) on Sep 30, 2003 at 16:54 UTC
    The eversion for CGI is 2.46...

    From perldoc CGI:

    To be safe, use the upload() function (new in version 2.47).  
    When called with the name of an upload field, upload() 
    returns a filehandle, or undef if the parameter is not a 
    valid filehandle.
    
    Also, you don't need to import :standard if you are using the OO interface.

    MB
      Well that solves that, eh?

      Apparently my reading of the revision history was less than thorough!

      Thank you very much for catching that matthewb!

      jg

Re: Undefined subroutine CGI::upload
by dws (Chancellor) on Sep 30, 2003 at 16:48 UTC
    Try changing   use CGI qw/:standard /; to   use CGI qw/:all/; That might be overkill, but it does the job for me.

      Thanks for the reply dws. Unfortunately the error continues with use CGI qw/:all/; jg
Re: Undefined subroutine CGI::upload
by dragonchild (Archbishop) on Sep 30, 2003 at 16:48 UTC
    Try recoding to only use the OO interface and change your use line to use CGI;. I remember reading that you shouldn't mix procedural and OO interfaces, though I'm not sure why.

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      I remember reading that you shouldn't mix procedural and OO interfaces, though I'm not sure why.

      Let's avoid the possibility of Cargo-Cult memes by supplying an actual reason. ;-)

      One reason is that when you mix the two interfaces you generally get 2 CGI objects. If data came back in a POST, only one will have the form data, leading to rather confusing results.