LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-02-2009, 03:10 PM   #1
Thor_2.0
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,227
Blog Entries: 23

Rep: Reputation: 279Reputation: 279Reputation: 279
Thumbs up Perly Issues


Hi,

Ok, no question from me this time, it's time to give something back for a change...

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:umper;
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
 
Old 01-02-2009, 06:10 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
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.

Last edited by Sergei Steshenko; 01-02-2009 at 06:33 PM.
 
Old 01-03-2009, 10:01 AM   #3
Thor_2.0
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,227
Blog Entries: 23

Original Poster
Rep: Reputation: 279Reputation: 279Reputation: 279
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
 
Old 01-04-2009, 08:43 PM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
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

Last edited by chrism01; 01-04-2009 at 08:45 PM.
 
Old 01-07-2009, 05:44 AM   #5
Thor_2.0
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,227
Blog Entries: 23

Original Poster
Rep: Reputation: 279Reputation: 279Reputation: 279
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
 
  


Reply

Tags
parameters, parsing, perl, perlscript


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ATI Ain in Wonder 8500DV LINUX LIVE boot issues (and TV issues) Flashmahn Linux - Hardware 1 03-09-2009 07:40 AM
Perly params Thor_2.0 Programming 15 12-05-2008 04:32 PM
Dell m1210 Linux issues (having issues getting network working under Ubuntu) thebert Linux - Newbie 4 03-14-2008 04:47 PM
X11 TTF Issues; OOo Font Issues Kenji Miyamoto Slackware 2 05-27-2005 07:30 PM
New to linux, so so lost, auto mounting issues, permissions issues slowhand22 Linux - Newbie 2 02-10-2005 10:41 AM


All times are GMT -5. The time now is 03:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration