0

以下是我的 Perl 代码:

use strict;
use File::Find;
use MIME::Base64;
use File::Temp qw(tempfile);

sub loadFiles();    #udf
sub mySub();        #udf

my @files = ();
my $dir = shift || die "Argument missing: directory name\n";
my $finalLoc;
my $filePath;
my $fileContents;
my $base64EncFile;
my $domain = "WTX";
my $devFilePath;
my $deviceDir;
my $position;
my $user   = "admin";
my $encPwd = "YzNKcGNtRnRZVEF4";
my $decPwd;
my $response;
my $temp;
my $tempFilename;
loadFiles();    #call

foreach (@files) {
    #take the file path into a variable
    $filePath = $_;
    #replace the '/' with '\' in the file path
    $filePath =~ s/\//\\/g;
    #take the file path into a variable
    $devFilePath = $_;
    #replace the '\' with '/' in the file path
    $devFilePath =~ s/\\/\//g;
    #perform string operation to derive a target file path
    $position = index( $devFilePath, "RPDM" );
    $deviceDir = "local:///" . substr( $devFilePath, $position );
    #open handle on file to read the contents
    open( FILE, "< $filePath" );
    #read the entire file into a variable, 'fileContents'
    $fileContents = do { local $/; <FILE> };
    #base64 encode the file contents
    $base64EncFile = encode_base64($fileContents);
    #replace the <CR><LF> characters in the file and flatten the base64 string
    $base64EncFile =~ s/[\x0A\x0D]//g;
    #printing file path
    print "FilePath=$filePath\n";

    #creating a temp file with 9 random characters at the end, example 'tempUKv1vqBTp'
    $temp = File::Temp->new(
        TEMPLATE => "tempXXXXXXXXX",
        UNLINK   => 0
    ) or die "Could not make tempfile: $!";
    $tempFilename = $temp->filename;
    #Printing temp file name
    print  "TempFileName=$tempFilename\n";

    #open the temp file for writing
   open(TEMP, ">$tempFilename");

    select(TEMP);
    while($base64EncFile){
    #??? HOW TO PRINT THE VARIABLE $base64EncFile CONTENTS INTO THE TEMP FILE ???
    }

    #creating a final request for sending to the web service
    my $dpString = "<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/' xmlns:dp='http://www.datapower.com/schemas/management'><env:Body><dp:request domain='$domain'><dp:set-file name='$deviceDir'>". $base64EncFile."</dp:set-file></dp:request></env:Body></env:Envelope>";
    #decode the encoded password
    $decPwd       = decode_base64($encPwd);
    system('C:\\apps\\curl-7.15.0\\curl.exe', '-#', '-k', '-u', "admin:$decPwd", '--data-binary', "$dpString", 'https://host/service/fileSet');
    print "-----------------------------------------------------------\n";
    close(TEMP);
    close(FILE);
}

sub loadFiles() {
    find( \&mySub, "$dir" );    #custom subroutine find, parse $dir
}

# following gets called recursively for each file in $dir, check $_ to see if you want the file!
sub mySub() {
    push @files, $File::Find::name
      if (/(\.xml|\.xsl|\.xslt|\.ffd|\.dpa|\.wsdl|\.xsd)$/i)
      ;    # modify the regex as per your needs or pass it as another arg
}

我要完成的任务是,给定上述 perl 程序的文件夹参数将对给定的 Web 服务端点进行递归调用。问题是 - 在 Perl 中使用 System 命令无法发送超过 32 Kb 的文件。在尝试在 perl 中使用 File::Temp 模块时,我不确定如何将变量的内容设置为临时文件(我使用 Perl 的第一周)。

实现这一目标的任何帮助都会有所帮助。谢谢!

4

1 回答 1

0

您是否在问如何将字符串写入打开的文件?

print $fh $string;

应该做的伎俩。

在您的示例中,这将转化为将 L62-65 替换为以下内容:

print TEMP $base64EncFile;
于 2012-05-22T20:12:00.667 回答