在Perl中使用File::Temp模块和系统命令
以下是我的Perl代码:在Perl中使用File::Temp模块和系统命令,perl,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 $f
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
}
使用严格;
使用File::Find;
使用MIME::Base64;
使用文件::Temp qw(tempfile);
子加载文件()#自由民主党
亚mySub()#自由民主党
我的@files=();
my$dir=shift | | die“参数丢失:目录名\n”;
我的$finalLoc;
我的$filePath;
我的$fileContents;
我的$base641文件;
my$domain=“WTX”;
我的$devFilePath;
我的$deviceDir;
我的美元头寸;
我的$user=“admin”;
my$encPwd=“yznkcgntrzvef4”;
我的$decPwd;
我的回答;
我的$temp;
我的$tempFilename;
加载文件()#呼叫
foreach(@files){
#将文件路径放入变量中
$filePath=$\ux;
#将文件路径中的“/”替换为“\”
$filePath=~s/\/\\/g;
#将文件路径放入变量中
$devFilePath=$\ux;
#将文件路径中的“\”替换为“/”
$devFilePath=~s/\/\//\//g;
#执行字符串操作以派生目标文件路径
$position=索引($devFilePath,“RPDM”);
$deviceDir=“local:///”.substr($devFilePath,$position);
#打开文件上的句柄以读取内容
打开(文件“<$filePath”);
#将整个文件读入变量“fileContents”
$fileContents=do{local$/;};
#base64对文件内容进行编码
$base64EncFile=encode_base64($fileContents);
#替换文件中的字符并展平base64字符串
$base64EncFile=~s/[\x0A\x0D]//g;
#打印文件路径
打印“FilePath=$FilePath\n”;
#创建一个临时文件,末尾有9个随机字符,例如“tempUKv1vqBTp”
$temp=文件::temp->新建(
模板=>“tempxxxxxxxx”,
取消链接=>0
)或死亡“无法生成临时文件:$!”;
$tempFilename=$temp->filename;
#打印临时文件名
打印“TempFileName=$TempFileName\n”;
#打开临时文件进行写入
打开(TEMP,“>$tempFilename”);
选择(温度);
而($base64EncFile){
#?如何将变量$base64EncFile内容打印到临时文件中???
}
#创建发送到web服务的最终请求
my$dpString=“”.$base64EncFile。“”;
#解码已编码的密码
$decPwd=解码基础64($encPwd);
系统('C:\\apps\\curl-7.15.0\\curl.exe'、'-#'、'-k'、'-u'、“admin:$decPwd”、'-data binary'、“$dpString”、'https://host/service/fileSet');
打印“--------------------------------------\n”;
关闭(温度);
关闭(文件);
}
子加载文件(){
查找(\&mySub,“$dir”)#自定义子例程查找、解析$dir
}
#下面是对$dir中的每个文件递归调用的,请检查$\以查看是否需要该文件!
副秘书长(){
推送@files,$File::Find::name
if(/(\.xml\.xsl\.xslt\.ffd\.dpa\.wsdl\.xsd)$/i)
##根据需要修改正则表达式或将其作为另一个参数传递
}
我试图完成的任务是,给定一个文件夹参数,上面的perl程序将对给定的web服务端点进行递归调用。问题是-在Perl中使用System命令无法发送超过32 Kb的文件。在尝试在perl中使用File::Temp模块时,我不确定如何将变量的内容设置到Temp文件中(我使用perl的第一周)
实现这一目标的任何帮助都将是有益的。谢谢 您是否在询问如何将字符串写入打开的文件
print $fh $string;
我们应该做到这一点
在您的示例中,这将转换为将L62-65替换为以下内容:
print TEMP $base64EncFile;
这个代码是我这个月见过的最丑陋的黑客。有更好的库可以在CPAN上发出HTTP/SOAP请求。考虑到我对Perl的熟悉程度,我不会否认这一点。如果你的评论给出了一个方向而不是一个开放的方向,那么你的评论会更有用。谢谢