2

I have used CGI::Simple in the past, for a general purpose upload processor. I apparently used an internal method called upload_fieldnames, which returns an array of field names that are uploads.

I am trying to adapt this code to use CGI.pm, because it is more commonly available for CentOS 7. How would I get a list of only file upload fields? All of the examples I've found use known field names, which I don't know in advance. Other fields might be submitted with the form, and I only need to act on the ones that provide a file upload.

jimp
  • 16,999
  • 3
  • 27
  • 36

1 Answers1

4

CGI.pm doesn't have an equivalent of the upload_fieldnames() method, but you can synthesise something by using a combination of the param() and upload() methods. param() will give you a list of all of the parameter names and you can use upload() to check whether or not each one is a file upload.

my @upload_fieldnames;

foreach (param()) {
  push @upload_fieldnames, $_ if upload($_);
}

Or, as a one-liner:

my @upload_fields = grep { upload($_) } param();

Update: I just wanted to add a comment on this.

I am trying to adapt this code to use CGI.pm, because it is more commonly available for CentOS 7

I'm not sure what your use case is here, but I strongly suspect that your time would be better spent working out how to install your required modules rather than working around the absence of certain modules.

If it helps you at all, I've just built an RPM of CGI::Simple and uploaded it to my RPM repository at rpm-mag-sol.com.

Dave Cross
  • 68,119
  • 3
  • 51
  • 97
  • For a library that blogs claim to be a drop in replacement for CGI.pm, I found CGI::Simple not very compatible at all. I had to rewrite most of my code to get it working. Your answer definitely pointed me in the right direction. Thanks! – jimp Dec 18 '18 at 21:34
  • @jimp: Being a drop-in replacement just means that CGI::Simple implements all of the methods in CGI. It doesn't mean that the opposite is also true. – Dave Cross Dec 19 '18 at 08:21
  • I see that, but I think some of the methods have different output. Maybe the two libraries diverged over time (not a bad thing). – jimp Dec 19 '18 at 16:04