in reply to File Upload - Strict Ref Issue

The error message is poor. It's being triggered by the the first argument of read. It should be a file handle, but isn't. Fix:
sub upload_file { my ($g, $q, $readpath, $writepath) = @_; my $rfh; if (!open($rfh, "<", $readpath)) { $g->errorpage($q, "Could not open file '$readpath': $!\n"); return 0; } my $wfh; if (!open($wfh, '>', $writepath)) { $g->errorpage($q, "Error creating file '$writepath': $!\n"); return 0; } binmode($rfh); binmode($wfh); while (read($rfh, my $buf, 4*1024)) { print $wfh $buf; } close($wfh); chmod 0600, $writepath; return 1; }

I got rid of your $| manipulation since you don't even use STDOUT.

I got rid of the sub's prototype. Not only should prototypes be avoided in general, it was completely wrong forcing you to bypass it elsewhere by wrongly adding "&" to sub calls.

Replies are listed 'Best First'.
Re^2: File Upload - Strict Ref Issue
by Heffstar (Acolyte) on Oct 29, 2009 at 15:11 UTC

    Holy crap. I was guided into perl and "learned" it from existing, and apparently poorly written, code. Prototyping like this was ALWAYS done, so I just followed suit...

    Should I be closing my read filehandle?

    I'll give this a shot, thanks VERY much for the input ikegami.

      They're lexically scoped, so they'll get closed when the sub exits. I left the close for the writer in case having an open handle prevents chmod from working.
        Gotcha, thanks.
Re^2: File Upload - Strict Ref Issue
by Heffstar (Acolyte) on Nov 10, 2009 at 18:50 UTC

    I tried to implement the open syntax you provided, but my version of perl doesn't support it. We're using 5.00503, which might be the problem.

    I've put it forward that we actually get our software updated for the first time in... who ever knows. We'll see what comes of it.

    Thanks again for your input.

      First time in a decade. 5.6 introduced the 3-arg open in 2000.