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

Difficult to explain perhaps.

I am using www::mech to fill out a form. There's an @array that holds a dynamic amount of content that needs to be filled in.

The form has 6 upload fields per page and I'm uploading every file from one of my directories into the page (looping over each set of 6 from the array until it submitted them all).

My question is, how do I submit the form for every 6 files while making sure the last set ONLY submits the number of remaining in case it's not an even set? I'm not really sure where to start the loop whether because I assume it has to follow $mech->submit_form( and before it submits.

The form allows 6 upload sets, each set has it's own upload file name and file category field (pic_file0-5 and pic_category0-5 respectively.). The username and email address only get passed once though.

I apologize if this sounds confusing but I have no idea where to begin the loop or how to have it submit every 6 files.

my @files = (); # assume there are files in here use WWW::Mechanize; my $mech = WWW::Mechanize->new(agent=>"Bot 1.0"); $mech->get( $url ); $mech->submit_form( $form_number => 1, fields => { # the image name and category here for each @files submitter_name => $username, submitter_email => $email, });

2006-03-14 Retitled by planetscape, as per Monastery guidelines
Original title: 'using mech with loops'

Replies are listed 'Best First'.
Re: using WWW::Mechanize with loops
by monarch (Priest) on Mar 14, 2006 at 03:55 UTC
    If the form permits upload of a file then why not start with the uncomplicated method of submitting one file at a time. Once you've got the hang of that, then it should be a relatively trivial matter to convert this into looping with more than one file at a time.

    One method of dealing with 6 files at a time is something like the following:

    while ( @files ) { my $num_to_submit = ( @files > 6 ? 6 : @files ); my $f1 = shift @files; my $f2 = shift @files; my $f3 = shift @files; my $f4 = shift @files; my $f5 = shift @files; my $f6 = shift @files; ... $mech->submit_form( ... fname1 => $f1 || '', fname2 => $f2 || '', ... fname6 => $f6 || '', ); }
      Hi.

      One file at a time would be a drag because after each submissions I need to click an email verification link. So I'd have to do some 400 email clicks instead of 1/6 of that submitting full forms.

      Can you explain what my $num_to_submit = ( @files > 6 ? 6 : @files ); Is doing?

        In this context @files returns the number of elements in the array @files. This ternary operator expression (@files > 6 ? 6 : @files) works like this:

        my $number_to_submit=@files; if @files > 6 then $number_to_submit = 6; else $number_to_submit = @files;

        So this limits the number of files to submit to 6 if there are more than 6 files in the array @files.

Re: using WWW::Mechanize with loops
by planetscape (Chancellor) on Mar 14, 2006 at 11:17 UTC

    Your description sounds to me very much like the web-based file manager used by the host for several of my sites... Fine for a couple files, but impossible for any large jobs.

    If this is the case, and your host permits FTP access, I strongly suggest you try a pre-packaged solution such as the one bobf suggested to me: FileZilla.

    HTH,

    planetscape
Re: using WWW::Mechanize with loops
by Anonymous Monk on Mar 14, 2006 at 03:45 UTC
    Sorry, for simplicity i'd like to add that the pic_category0-5 will be the same for every file. So just assume we're using $category as the value for it, just so people don't ask.