#!/usr/bin/perl -w use warnings; use strict; use Spreadsheet::ParseExcel; # Something to consider for future scripts. # # This call (my $oExcel = new Spreadsheet::ParseExcel;)to new() is used to create a # reference to Spreadsheet::ParseExcel as an object which is what you want to do # so you can use all the methods available within Spreadsheet::ParseExcel, # but it never gets used other than here. # Later on you call, # my $oBook = Spreadsheet::ParseExcel::Workbook->Parse($filename);, # which creates its own separate object that you DO use for the manipulation of # your data and so on. # # ***I understand deadlines*** # My only squeek about this has to do with the use of memory. In this # script it doesn't seem as though the assignment of memory will cause # any problems, but the practice of declaring objects and never using # them may, in the future. Each object created uses memory, and in a hosting # situation, your resources may be limited, so you need to trim the fat after # you get the script up and running. # # Try this script without the call (my $oExcel = new Spreadsheet::ParseExcel;) # since the later call (my $oBook = Spreadsheet::ParseExcel::Workbook->Parse($filename);) # is the object being used. You will find that the script won't eat as much memory, # and it will run faster without the useless time wasted on the assignment. # # The call (my $oBook = Spreadsheet::ParseExcel::Workbook->Parse($filename);) # is the object "$oBook" created that you ARE using, not $oExcel. # my $oExcel = new Spreadsheet::ParseExcel; #commented out my $dir = $ARGV[1]; chomp $dir; unless (-d $dir) { print "Can't find directory $dir"; exit; } my $filename = "$dir/$ARGV[0]"; chomp $filename; unless (-e $filename) { print "Can't find file $filename"; exit ; } my $fullfilename = "$filename.txt"; ## start work print "Converting..."; #1.1 Normal Excel97 open E2T, ">", $fullfilename or die $!; my $oBook = Spreadsheet::ParseExcel::Workbook->Parse($filename); #Here is the object that you are using!!! my($iR, $iC, $oWkS, $oWkC); foreach my $oWkS (@{$oBook->{Worksheet}}) { print "--------- SHEET:", $oWkS->{Name}, "\n"; print E2T $oWkS->{Name}, "|"; next unless defined $oWkS->{MinRow} and defined $oWkS->{MaxRow}; for my $iR ($oWkS->{MinRow} .. $oWkS->{MaxRow}) { print E2T "\n"; for my $iC ($oWkS->{MinCol} .. $oWkS->{MaxCol}) { $oWkC = $oWkS->{Cells}[$iR][$iC]; unless (!defined $oWkC){ print "( $iR , $iC ) =>", $oWkC->Value, "\n" if($oWkC); print E2T $oWkC->Value; } print E2T "|"; } } print E2T "\n"; } close E2T; ## UPDATE ## add the require for the second file here require "name_of_second_file.pl"; print "Finished!"; exit; 1; #Don't forget this -- added to tell Perl that the file has been completely read and "use" or "require" has been fulfilled #### #!/usr/bin/perl # Look at the (sub xmlrow), here again you are creating # scalars (my $ostar="${$colname}$estar$colname\>\n"; # Note the backslash to escape the > of the closing tags. # # However; The call to print as you have written it will be # much faster, and use less memory. So if you are not going to # modify this any further, get rid of the scalars. ## UPDATE ## How about using INFILE and OUTFILE for your ## filehandles instead to avoid ## any problems within the world of Perl. ## Or, FILE1 FILE2 #this is the out from the previous script $IN = "someFileName.txt"; $OUT = "someOutFile.xml"; open (OUT, ">$OUT"); open(IN) or die ("Cannot open file ($IN)"); print OUT < TOP $id = 0; @columns = (category,id,code,title,summary,prereq,group,subgroup,sequence,rolemandatory,rolerecommended,roleoptional,url,modality,"length"); foreach $row (){ ($category,$code,$title,$summary,$prereq,$group,$subgroup,$sequence,$rolemandatory,$rolerecommended,$roleoptional,$url,$modality,$length) = split ('\|', $row); print OUT "\n"; foreach $_ (@columns){ &xmlrow($_); } print OUT "\n"; $id++; } print OUT "\n"; sub xmlrow{ my $colname = shift(@_); my $ostar="${$colname}\n"; } 1; #added to tell Perl that the file has been completely read and "use" or "require" has been fulfilled