Perly Issues
Hi,
Ok, no question from me this time, it's time to give something back for a change... :cool: I refer to my question I launched a while back, titled "Perly params" tags "perlscript, parsing, parameters, perl" Ok, the situation was as follows: I have an application written in Java. There are some external scripts written in PERL. Each of them takes arguments that it has to process. Of course, these arguments can contain spaces, and this is where PERL skips out. Well, this was where I wanted something PERL was'nt designed to give me... I refer to some info I picked up at http://www.javaworld.com/javaworld/j...ps.html?page=4 More specific: One final pitfall to cover with Runtime.exec() is mistakenly assuming that exec() accepts any String that your command line (or shell) accepts. Runtime.exec() is much more limited and not cross-platform. This pitfall is caused by users attempting to use the exec() method to accept a single String as a command line would. The confusion may be due to the fact that command is the parameter name for the exec() method. Thus, the programmer incorrectly associates the parameter command with anything that he or she can type on a command line, instead of associating it with a single program and its arguments. A workaround was called for... A snippet: Assume for arg1 = "12/5/2008 15:00 CET The society of Mind Marvin Minsky 90 351 0429 3" this being four chunks of info: 12/5/2008 15:00 GMT The society of Mind Marvin Minsky ISBN 90 351 0429 3 Thje command is: strCommand = new String("perl " + strListHome + "S5a.pl " + arg1); try { Scanner scan = new Scanner(Runtime.getRuntime().exec(strCommand).getInputStream()); while(scan.hasNextLine()) { doFeedBack(scan.nextLine()); } } catch(RuntimeException e) { doFeedBack("Runtime exception" + e.getMessage()); } catch(Exception e) { doFeedBack("Runtime (other) exception " + e.getMessage()); } Of course, this is bound to fail, as the parser chopps the data up...at the spaces. The (possible) workaround is to make a void to sent all the args one line per line to a text file, delimited by a newline. The PERL S5a.pl then reads the text file and processes where needed...in this case writing an XML to a file... The command then is reduced to calling the PERL S5a.PL with the desired folder as agrument, in this case the raw input is there, written by the Java Application...of course... ---> start snippet: use Data::Dumper; use vars; my $data_file="booklist.dat"; my $detailstream = "raw_input.txt"; my $line; $folder = $ARGV[0]; chdir $folder; open(PFH, ">>$data_file") or die "Could not append to $data_file : $!\n"; open(FILE1, "<$detailstream") or die "No input details found...\n"; while($line = readline(FILE1)) { print PFH "<book>\n"; chomp($line); print PFH "<date>$line</date>\n"; $line = readline(FILE1); chomp($line); print PFH "<title>$line</title>\n"; $line = readline(FILE1); chomp($line); print PFH "<author>$line</author>\n"; $line = readline(FILE1); chomp($line); print PFH "<isbn>$line</isbn>\n"; print PFH "</book>\n"; } close(PFH); close(FILE1); ----> end snippet Bigbig tnx to Sergei Steshenko, Disillusionist and chrism01 for their input and patience!!!! Oh, and a fine new year 4 all of U!!! Thor |
I am not sure I understand what you wanted to say.
But here is a number of tips for you:
Here is a quick example: Code:
sergei@amdam2:~/junk> cat -n data_importer.pl |
Hi,
Tnx! However, the considerations are the following: - the XML file is needed to allow other apps to read the data, it has to transport the data... - I did'nt use any extra libraries so as not to demand anything extra from the host system - the PERL scripts are there to allow editing where needed (by a receiving system Root)... So, I had to use XML... Tnx!! :))) Thor |
I would recommend adding error handling (inc $!) to chdir(), ALL open() & close() statements. That way you know what caused the error instead of assuming...
oh, and use strict; use warnings; # or the -w flag |
Indeed Chris! Of course to survive the harsh "winter of the deploy-world" error handling is an absolute M-U-S-T!!!!
But all in all, a solution to a nasty problem... I hope it was to some info to someone... Thor :) |
All times are GMT -5. The time now is 08:54 PM. |