LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perly Issues (https://www.linuxquestions.org/questions/programming-9/perly-issues-694544/)

ButterflyMelissa 01-02-2009 02:10 PM

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

Sergei Steshenko 01-02-2009 05:10 PM

I am not sure I understand what you wanted to say.

But here is a number of tips for you:
  1. Perl is not Java - mostly in a good sense;
  2. Perl easily understands data in Perl format, though Java can compile on the fly too;
  3. The easiest and safest way to feed data into Perl is through a file in Perl (and not XML or whatever) format.

Here is a quick example:

Code:

sergei@amdam2:~/junk> cat -n data_importer.pl
    1  #!/usr/bin/perl -w
    2
    3  use strict;
    4
    5  my $hash_ref = require "./data.prl";
    6
    7  foreach my $key(sort keys %{$hash_ref})
    8    {
    9    my $value = ${$hash_ref}{$key};
    10    warn "\$key=$key, |\$value|=|$value|"; # '|'s show the limits for clarity
    11    }
    12
sergei@amdam2:~/junk> cat -n data.prl
    1  use strict;
    2
    3
    4  # anonymous hash reference to be exported
    5  {
    6  foo => "whatever you like",
    7  bar => "and something you need"
    8  };
sergei@amdam2:~/junk> ./data_importer.pl
$key=bar, |$value|=|and something you need| at ./data_importer.pl line 10.
$key=foo, |$value|=|whatever you like| at ./data_importer.pl line 10.
sergei@amdam2:~/junk>

I.e. your safest way is to forget about command line args at all and to use Perl format for Per data export and import.

ButterflyMelissa 01-03-2009 09:01 AM

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

chrism01 01-04-2009 07:43 PM

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

ButterflyMelissa 01-07-2009 04:44 AM

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.