Thank you for the pointer to use File::Temp module. Able to generate a temporary file name but not sure how to write the content from a string into the temp file, simple assignment does not work viz.,
$tempFile = $stringValue;
Following is my code, please advice:
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 = "ABC";
my $devFilePath;
my $deviceDir;
my $position;
my $user = "admin";
my $encPwd = "kaosapdkasd=";
my $decPwd;
my $response;
my $temp;
my $tempFilename;
loadFiles(); #call
#map ({ print "$_\n"; } @files);
foreach (@files) {
$filePath = $_;
$filePath =~ s/\//\\/g;
$devFilePath = $_;
$devFilePath =~ s/\\/\//g;
$position = index( $devFilePath, "RPDM" );
$deviceDir = "local:///" . substr( $devFilePath, $position );
open( FILE, "< $filePath" );
$fileContents = do { local $/; <FILE> };
$base64EncFile = encode_base64($fileContents);
$base64EncFile =~ s/[\x0A\x0D]//g;
#creating a temp file to be able to set large files (~500 kb)
$temp = File::Temp->new(
TEMPLATE => "tempXXXXXXXXX",
UNLINK => 0
) or die "Could not make tempfile: $!";
close $temp or die "Could not close tempfile: $!";
$tempFilename = $temp->filename;
print "$filePath\n";
print "$tempFilename \n";
my $dpString =
"<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/' x
+mlns:dp='http://www.datapower.com/schemas/management'><env:Body><dp:r
+equest domain='$domain'><dp:set-file name='$deviceDir'>"
. $base64EncFile
. "</dp:set-file></dp:request></env:Body></env:Envelope>";
$decPwd = decode_base64($encPwd);
#***HOW TO GET THE CONTENTS FROM $dpString INTO $tempFilename OTHER TH
+AN SIMPLE ASSIGNMENT ?
$tempFilename = $dpString;
#print "\n$dpString\n";
#$response = system('C:\\apps\\curl-7.15.0\\curl.exe', '-#', '-k', '-u
+', "admin:$decPwd", '--data-binary', "$dpString", 'https://hostxyz:55
+50/service/mgmt/current');
# print "$response\n";
system(
'C:\\apps\\curl-7.15.0\\curl.exe',
'-#',
'-k',
'-u',
"admin:$decPwd",
'--data-binary',
"$dpString",
'https://hostxyz:5550/service/mgmt/current'
);
close(FILE);
print "\n---------------------------------------------------------
+--\n";
}
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
}
|