Ìû - Òî÷êà Êèïåíèÿ

Ìû - êîìàíäà ñïëî÷åííûõ èäååé


sub PARSE_MULTIPART {
    my ($STREAM,$boundary,$HANDLE,$PROCESS) = @_;
    open my $DH,'>',do{\(my $null)}; # devnull
    local $_ = "--\r\n";
    local $/ = \8192;
    my ($BR,$BL) = map {qr{^ (.*?) \Q$_\E }sx,length} map {"\r\n--$_"} $boundary;
    /^/;
    while ( (length) < length($_.=<$STREAM>) and $PROCESS->($.,${$/}) ){
            (print $DH
                m{$BR}
                      ? $1
                      : substr($_,0,-$BL,'')
            )
             &&
                $+[0]
        ...
                m{^
                       \r\n Content-Disposition       :\ form-data
                                                      ;\ name     = (?:"([^"]*)"|(\S+))
                   (                                  ;\ filename = (?:"([^"]*)"|(\S+)) )?
                   (?: \r\n Content-Type              :\            (.+?)               )?
                   (?: \r\n Content-Transfer-Encoding :\            (.+?)               )?
                       \r\n
                       \r\n
                }sx
             &&
                ($DH = $HANDLE->({
                          'name'     => length $1 ? $1 : $2      ,
                    $3 ? ('filename' => length $4 ? $4 : $5) : (),
                    $6 ? ('type'     =>             $6     ) : (),
                    $7 ? ('encode'   =>             $7     ) : (),
                }))
             ||
                m{^--};
        $+[0] and substr($_,0,$+[0],''),/^/,redo;
    }
}


There is more than one way to do it. Larry Wall.