Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

CGI.pm file uploads in very generic script

by amelinda (Friar)
on Nov 14, 2001 at 04:45 UTC ( #125185=perlquestion: print w/replies, xml ) Need Help??

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

Look! I'm use CGI;ing! And you all thought I couldn't do it. Sadly, it is still legacy code. Arkayne legacy code.

So, it looks like they tried to implement something that was very generic, and attempts to handle potential (but not certain) multiple file uploads. Too bad it ends up with only the last one.

So, for the sake of sanity, I'm trying to do as little modification as possible. I'm looking at the CGI function append(); as a possible near-drop-in replacement for the use of param('foo','new value');. At this point, I need to know one thing: does append(); do a scalar (ie, smoosh into one big string) or list (ie, push value onto the end of an array) append?

FWIW, here is some sample code:

foreach my $x ($q->param()) { # if a file upload: my $filename = $q->param($x); # loop read in the file: my $filedata = $buffer; $q->param('_filename', $filename); $q->param('_filedata', $filedata); } # a bunch of other stuff. # this is to :sigh: mail the file to someone remote $q->param('_filedata',encode_base64($q->param('_filedata'))); # stuff the data into an attachment by hand

As you'll notice, the values of $q->param('_filedata'); and $q->param('_filename'); are overwritten every time through this loop. I'm not sure what the heck the original author was thinking, but he's (of course) not here now.

So, I'm hoping that $q->append(); will help me here by making an array out of _filedata and _filename. Think it will?

Replies are listed 'Best First'.
Re: CGI.pm file uploads in very generic script
by pjf (Curate) on Nov 14, 2001 at 04:58 UTC
    G'day amelinda,

    CGI's append function varies in behaviour depending upon what it's second argument is. If passed a list reference as a second argument, it appends all those values onto the list of values associated with that given parameter. If the second argument is a scalar, then only that scalar is appended. The third and subsequent arguments are silently ignored.

    Here's some example code:

    $q->append("foo","hello"); # Adds "hello" to foo's list of v +alues. $q->append("foo","hello","world"); # As above, world is ignored. $q->append("foo",["hello","world"]); # appends both hello and world to + foo's values.

    Hope that this helps clarify things.

    Cheers,
    Paul

      I think I understand what you mean. But I'd like to clarify it just a little further.

      So, if we have:

      $q->param('foo', 'junk'); $q->append('foo', "$something");
      is 'foo' now "junk$something" or is it ["junk", "$something"]?
        The latter. ["junk","$something"];

        If you want to turn that into the first (ie, one big concatenated string), you can do this:

        $q->param("foo",join("",$q->param("foo")));

        Cheers,
        Paul

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2020-10-19 23:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (207 votes). Check out past polls.

    Notices?