Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Perlscript is not generating output in files

by manishrathi (Beadle)
on Jun 23, 2011 at 16:26 UTC ( #911107=perlquestion: print w/ replies, xml ) Need Help??
manishrathi has asked for the wisdom of the Perl Monks concerning the following question:

#!C:\Interwoven\TeamSite/iw-perl/bin/iwperl use File::Copy; use TeamSite::Config; use XML::DOM; use XML::Parser; use open ':utf8'; use TeamSite::DCRnode; use TeamSite::DCRparser; use Time::localtime; use File::Basename; use Encode; use strict; use warnings; open(LOG ,">C:/Interwoven/TeamSite/tmp/Cloning.log"); my $Workarea = ""; my $fileLine = $ARGV[0]; if ($fileLine ne "") { my @fline = split("####",$fileLine); my @clone = split("~",$fline[1]); for (my $c=1;$c<= $#clone ;$c++) { my $cloneName = $clone[$c]; my $pathSource = $fline[0]; print LOG "Arguments Provided :-\n[$cloneName]\n[$pathSour +ce]\n"; print LOG " The source of the HTMLS:- $pathSource \n"; print LOG " The dest of the HTMLS:- $cloneName \n"; my $path = "Y:/"; $pathSource =~ s/\\/\//g; my @pathSourceArr = split(/\//,$pathSource); my $source_branch =$pathSourceArr[$#pathSourceArr-1]; print LOG " The source of the HTMLS after replacement :- $ +pathSource \n"; my @spiltSource = split("/",$pathSource); $pathSource = "Y:/"; my $templateData = "Y:/"; print LOG "The splitted source path :- \n @spiltSource\n"; for (my $i=3;$i < $#spiltSource ;$i++) { $path .= $spiltSource[$i]."/"; $pathSource .= $spiltSource[$i]."/"; if (($spiltSource[$i] ne "html")&& ($spiltSource[$i] n +e "content") ) { $templateData .= $spiltSource[$i]."/"; } if($spiltSource[$i] eq "WORKAREA") { $Workarea = $spiltSource[$i+1]; } } print LOG "Workarea Name :- [$Workarea]\n"; my $xmlFile = $templateData . "xml/" . $Workarea . "/siteN +ame.xml"; print LOG " siteName xml file Path :-[$xmlFile]\n"; my $AddToMetadata = "Yes"; if(-e $xmlFile) { my $parser=new XML::DOM::Parser; my $doc=$parser->parsefile($xmlFile); my $nodes = $doc->getElementsByTagName("itemID"); my $nname = $doc->toString; my $rootnode= TeamSite::DCRnode->new($nname); my @new_node = $rootnode->get_node_list('itemID'); + # getting the array of all the siteName nodes. my $n = $nodes->getLength; for (my $i = 0; $i <= $#new_node; $i++) { my $att_value = $new_node[$i]->value(); if( $att_value eq $cloneName ) { print LOG " +****** siteName Updation Starts Here(If File exists in Xml) ***** + \n"; $new_node[$i]->set_inner_xml($cloneName); my $pro_node_xml = $rootnode->get_xml(); print LOG "\nNew Meta Data XML :-\n $pro_n +ode_xml \n"; open(FILE,">$xmlFile"); print FILE "<?xml version=\"1.0\" encoding +=\"UTF-8\"?>"; print FILE $pro_node_xml; close(FILE); $AddToMetadata = "No"; print LOG "After checking the siteName\.xm +l AddToMetadata or not :- [$AddToMetadata] \n"; print LOG " +****siteName Updation Ends Here**** "; last; } } if ($AddToMetadata eq 'Yes') { print LOG " +*****siteName Updation Starts Here(If File doesnot exists in the Xml) + ***** \n"; $rootnode->set_inner_xml($rootnode->get_in +ner_xml() . "\n<itemID>$cloneName</itemID>\n"); my $pro_node_xml = $rootnode->get_xml(); print LOG "\nNew Meta Data XML :-\n $pro_n +ode_xml \n"; open(FILE,">$xmlFile"); print FILE "<?xml version=\"1.0\" encoding +=\"UTF-8\"?>"; print FILE $pro_node_xml; close(FILE); print LOG " +*****siteName Updation Ends Here**** "; } } else { print LOG " **** sit +eName Creation Starts Here(IF siteName.xml does not exists) **** + \n"; open(FILE,">$xmlFile"); print FILE "<?xml version=\"1.0\" encoding=\"UTF-8 +\"?>"; print FILE "<content locale=\"$Workarea\">\n<itemI +D>$cloneName</itemID>\n</content>"; close(FILE); print LOG " *****sit +eName Updation Ends Here**** "; } $path .= $cloneName ; $pathSource .= $spiltSource[$#spiltSource]; print LOG "The constructed source and destination path is +:- \n $pathSource and $path \n"; print LOG "The constructed templatedata path is :- \n $tem +plateData \n"; if( -e $path){} else{ mkdir($path,"0777") or die "Unable to create folder: $!"; } #opendir(DIR, $from_dir); #my @dir1 = readdir(DIR); #closedir(DIR); sub copy_recursively { my ($from_dir,$to_dir) = @_; opendir(DIR, $from_dir) or die "Could not open dir '$f +rom_dir': $!"; my @dir = readdir(DIR); for(my $j=2 ; $j <= $#dir; $j++){ my $source = "$from_dir/$dir[$j]"; my $destination = "$to_dir/$dir[$j]"; next if($dir[$j] eq "." || $dir[$j] eq ".."); if (-d $source) { print LOG "The source and destination in the d +irectory section :-\n $source and $destination\n"; mkdir($destination,"0777") or die "mkdir '$des +tination' failed: $!" if not -e $destination; copy_recursively($source, $destination); } else { print LOG "The source and destination in t +he File section :-\n $source and $destination\n"; open(FILE,"<", $source); my @file_content = <FILE>; close(FILE); for(my $u=0;$u<=$#file_content;$u++) { $file_content[$u] = s/logo-$source_bra +nch/logo-$cloneName/g ; print LOG "$file_content[$u]"; } open(FILEOUT,">", $destination); for(my $v=0;$v<=$#file_content;$v++) { print FILEOUT "$file_content[$v]"; } #copy($source, $destination) or die "copy +failed: $!"; my $cmd = "iwextattr -g TeamSite/Templatin +g/PrimaryDCR $source"; my $dcrName = `$cmd`; my $cmdDcrType = "iwextattr -g TeamSite/Te +mplating/PrimaryDocumentType $source"; my $dcrType =`$cmdDcrType`; print LOG "CMD command execueted with the +HTML for DCR Type :- [$cmdDcrType]\n\n$dcrType\n"; if($dcrName ne "" && $dcrType ne "") { my @dcrNameSplit = split("/",$dcrName) +; print LOG "CMD command execueted with +the HTML for DCR Name :- [$cmd]\n\n[$dcrName]\n\n@dcrNameSplit\n"; my $dcrPath = "$templateData"; $dcrPath .= "templatedata/$dcrType/dat +a/$cloneName"; mkdir($dcrPath,"0777")or die "mkdir '$ +dcrPath' failed: $!" if not -e $dcrPath; $dcrPath .= "/"; my $setDCRName = "$cloneName"; for (my $k=1;$k<$#dcrNameSplit ;$k++) +{ $dcrPath .= $dcrNameSplit[$k]; mkdir($dcrPath,"0777")or die "mkdi +r '$dcrPath' failed: $!" if not -e $dcrPath; $dcrPath .= "/"; $setDCRName .= "/$dcrNameSplit[$k] +" } my @splitDcrType = split("/",$dcrType) +; my $PT = $splitDcrType[$#splitDcrType] +."\.xsl"; $dcrPath .= $dcrNameSplit[$#dcrNameSpl +it]; $setDCRName .= "/$dcrNameSplit[$#dcrNa +meSplit]"; print LOG "\n The New DCR Pathh is:- $ +dcrPath\n"; my $dcrSource = "$templateData"; $dcrSource .= "templatedata/$dcrType/d +ata/$dcrName"; print LOG "\n The DCR Pathh is:- $dcr +Source\n"; copy($dcrSource,$dcrPath) or die "copy + of the dcr failed: $!"; my $cmd1 = "iwextattr -s TeamSite/Temp +lating/DCR/Type=$dcrType $dcrPath"; my $new1 = `$cmd1`; my $cmdToSet = "iwextattr -s TeamSite/ +Templating/PrimaryDCR=$setDCRName $destination"; my $newDcrName = `$cmdToSet`; my $cmd2 = "iwextattr -s TeamSite/Temp +lating/PrimaryDocumentType=$dcrType $destination"; my $new2 = `$cmd2`; my $cmd3 = "iwextattr -s TeamSite/Temp +lating/PrimaryPT=$PT $destination"; my $new3 = `$cmd3`; print LOG "\n The New output Type[$new +DcrName][$cmdToSet] is set for the DCR :- $dcrPath\n"; } } } closedir(DIR); return; } sub alter_content_for_logo{ } copy_recursively($pathSource, $path); } } close(LOG); #unlink($fileName); exit;
In this script, we are cloning an entire folder and ceating a new folder. All the folders within folder and files within them are all created in new folder. SOme of the files contain logo of old folder which is renamed to new logo and we have a code for than in this script. which is as follows
sub copy_recursively { my ($from_dir,$to_dir) = @_; opendir(DIR, $from_dir) or die "Could not open dir '$f +rom_dir': $!"; my @dir = readdir(DIR); for(my $j=2 ; $j <= $#dir; $j++){ my $source = "$from_dir/$dir[$j]"; my $destination = "$to_dir/$dir[$j]"; next if($dir[$j] eq "." || $dir[$j] eq ".."); if (-d $source) { print LOG "The source and destination in the d +irectory section :-\n $source and $destination\n"; mkdir($destination,"0777") or die "mkdir '$des +tination' failed: $!" if not -e $destination; copy_recursively($source, $destination); } else { print LOG "The source and destination in t +he File section :-\n $source and $destination\n"; open(FILE,"<", $source); my @file_content = <FILE>; close(FILE); for(my $u=0;$u<=$#file_content;$u++) { $file_content[$u] = s/logo-$source_bra +nch/logo-$cloneName/g ; print LOG "$file_content[$u]"; } open(FILEOUT,">", $destination); for(my $v=0;$v<=$#file_content;$v++) { print FILEOUT "$file_content[$v]"; }
When I am not running this piece of code, all the content in files come up perfect. But when I run the script with this piece of code, content in file does not get filled and all the files are zero size files. What is the reason that files are not getting populated with content ?

Please dont worry about the TeamSite packages used in here. Problem is with the piece of code and the way files are opened, arrayed and written back.

Thanks

Comment on Perlscript is not generating output in files
Select or Download Code
Re: Perlscript is not generating output in files
by kennethk (Monsignor) on Jun 23, 2011 at 16:35 UTC
    Please read How do I post a question effectively?. There is a lot of material in your post that, by your own admission, is not pertinent to the issue you are experiencing. It also doesn't help that we can't easily replicate your issue because of the lack of input.

    Regardless, I suspect your issue is that:

    $file_content[$u] = s/logo-$source_branch/logo-$cloneName/g ;

    should read

    $file_content[$u] =~ s/logo-$source_branch/logo-$cloneName/g ;

    See Binding Operators if you are unsure why.

Re: Perlscript is not generating output in files
by Fletch (Chancellor) on Jun 23, 2011 at 17:59 UTC

    ALWAYS CHECK THE RETURN VALUE OF SYSTEM CALLS SUCH AS open.

    open( my $fh, '>', "bar" ) or die "Can't write to bar: $!\n";

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://911107]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2014-08-22 02:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (145 votes), past polls