在Perl中使用File :: Temp模块和System命令

时间:2012-05-22 19:48:12

标签: perl

以下是我的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的第一周)。

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

1 个答案:

答案 0 :(得分:0)

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

print $fh $string;

应该这样做。

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

print TEMP $base64EncFile;