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

Hi, I'm writing a test script that should simulate uploading a file to a CGI form. Is there a way to create a filehandle that behaves like CGI filehandles, returning a string in scalar context and a filehandle in filehandle context? CGI.pm itself does it using a private class Fh, which I don't want to steal. Thanks!

Replies are listed 'Best First'.
Re: CGI-style filehandles
by merlyn (Sage) on May 13, 2005 at 20:07 UTC
    I finally just got file upload simulation working with CGI::Prototype::Mecha (because the $client wanted tests to test an upload form). Take a look at CGI/Prototype/Mecha.pm in that distro, and see how I turn an HTML::Form object (including file parameters) into a CGI.pm object. And it works nicely. For example, to test an upload in the middle of a mechanize run:
    ok($m->submit_form (fields => {file_name_add1 => '/etc/passwd', attachment_desc_add1 => 'Our password file', }, button => 'add_commit_item', ), "submitted form"); is ($m->status, 200, "attachment page status OK after submitting a fil +e");
    where file_name_add1 is a input-type-file. In other words, it's completely transparent.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.