in reply to Bring two scripts together?

Hello lakeTrout,

Take a look at these suggestions as well as the other posts!

In my own adventures of programming I have run into some difficult debugging situations due to my mis-understanding of how Perl assigns and uses memory.

I hope I can save you from problems you may encounter in the future if you stay the course of your current programming style.

I have included your scripts with comments inside. I hope this can help you.

The first file

#!/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 u +sed 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::ParseEx +cel, # 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 manipu +lation of # your data and so on. # # ***I understand deadlines*** # My only squeek about this has to do with the use of memory. In thi +s # 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 h +osting # situation, your resources may be limited, so you need to trim the fa +t after # you get the script up and running. # # Try this script without the call (my $oExcel = new Spreadsheet::Pars +eExcel;) # 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 assign +ment. # # The call (my $oBook = Spreadsheet::ParseExcel::Workbook->Parse($file +name);) # 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); #Her +e 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 com +pletely read and "use" or "require" has been fulfilled

The second file

#!/usr/bin/perl # Look at the (sub xmlrow), here again you are creating # scalars (my $ostar="<xtag:"; and my $estar="</xtag:";) # that are never used. And, maybe you have a plan # for them in the future that I am not aware of! But, this # time, we are part of a loop sub routine. Each time it is # called, it creates and assigns memory to these scalars. # # I think you wanted to do this: # print OUT "$ostar$colname\>${$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; <?xml version="1.0" encoding="ISO-8859-1"?> <xtags:some-node xml:lang="en"> TOP $id = 0; @columns = (category,id,code,title,summary,prereq,group,subgroup,seque +nce,rolemandatory,rolerecommended,roleoptional,url,modality,"length") +; foreach $row (<IN>){ ($category,$code,$title,$summary,$prereq,$group,$subgroup,$sequence,$r +olemandatory,$rolerecommended,$roleoptional,$url,$modality,$length) = + split ('\|', $row); print OUT "<star:course>\n"; foreach $_ (@columns){ &xmlrow($_); } print OUT "</xtag:course>\n"; $id++; } print OUT "</star:learning-paths>\n"; sub xmlrow{ my $colname = shift(@_); my $ostar="<xtag:"; my $estar="</xtag:"; print OUT "<xtag:$colname>${$colname}</xtag:$colname>\n"; } 1; #added to tell Perl that the file has been completely read and "use +" or "require" has been fulfilled

UPDATE: There was an earlier suggestion that you add a require "second_file_name.pl" statement in order to append the second script. Simply place the require statement above the print "Finished\n"; statement in your first file. then, remove the #!/usr/bin/perl at the top of the second file.

I placed a few more notes and suggestions in the readmores for you to look at. Hope this helps!!

Remember: It's not a bug, It's a feature!! Credit must be given to the original Author of this tag line, but I can't remember who it is!

Replies are listed 'Best First'.
Re^2: Bring two scripts together?
by lakeTrout (Scribe) on Jan 22, 2007 at 17:54 UTC
    Howdy!
    I really appreciate your suggestions and the amount of time you (and others) must have spent to help me. I have the two scripts running a little cleaner now, but can't seem to bring them together. Pardon is this is just an ignorant question of if I've just been looking at this too long. I appreciate the help or tips.

    -lT