LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 11-29-2008, 11:59 AM   #1
Thor_2.0
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,217
Blog Entries: 23

Rep: Reputation: 279Reputation: 279Reputation: 279
Perly params


Hi,

Ok, first off, I hope this IS the (more or less) proper spot to pose this question...if not, "mucho sorry"... ;-)

I am building an application in Java. Because I need some functionality the host system has to offer, I use PERL to do that for me. Basicalliy, the user sets some data, one portion is a description, the user is allowed to enter spaces as well.

A snippet:

---------------------------------------------------------------------------
strCommand = new String("perl " + strScriptHome + "S5a.pl " + strDescr + " ";
try
{
Scanner scan = new Scanner(Runtime.getRuntime().exec(strCommand).getInputStream());
while(scan.hasNextLine())
{
System.out.println(scan.nextLine());
}
}
catch(RuntimeException e)
{
(handle the exceptionhere)
}
---------------------------------------------------------------------------

Explanation: in the first line, the command gets build up, the following line(s) execute this tru the Jara.Runtimer, errors are grabbed by the try/catch...

In the PERL S5a.pl, I call $ARGV[0] to pick up the description, but the routine only uses the first word, and calls it quits after that. Obviously, the _space_ is the delimiter.

$descr = $ARGV[0];

How do I bypass this? Escaping/unescaping? Some magic(k) formula ;-) (no dancing naked in the full moon, it's winter here, in Flanders/Belgium, it's way to cold...besides, I dont have a decent tan line....)

LOL

Tnx 4 the HLP

Thor
 
Old 11-30-2008, 05:04 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by linusr@flanders View Post
Hi,

Ok, first off, I hope this IS the (more or less) proper spot to pose this question...if not, "mucho sorry"... ;-)

I am building an application in Java. Because I need some functionality the host system has to offer, I use PERL to do that for me. Basicalliy, the user sets some data, one portion is a description, the user is allowed to enter spaces as well.

A snippet:

---------------------------------------------------------------------------
strCommand = new String("perl " + strScriptHome + "S5a.pl " + strDescr + " ";
try
{
Scanner scan = new Scanner(Runtime.getRuntime().exec(strCommand).getInputStream());
while(scan.hasNextLine())
{
System.out.println(scan.nextLine());
}
}
catch(RuntimeException e)
{
(handle the exceptionhere)
}
---------------------------------------------------------------------------

Explanation: in the first line, the command gets build up, the following line(s) execute this tru the Jara.Runtimer, errors are grabbed by the try/catch...

In the PERL S5a.pl, I call $ARGV[0] to pick up the description, but the routine only uses the first word, and calls it quits after that. Obviously, the _space_ is the delimiter.

$descr = $ARGV[0];

How do I bypass this? Escaping/unescaping? Some magic(k) formula ;-) (no dancing naked in the full moon, it's winter here, in Flanders/Belgium, it's way to cold...besides, I dont have a decent tan line....)

LOL

Tnx 4 the HLP

Thor
I am not sure what your problem is. It seems it's not related to Perl at all.

If you need something with whitespaces to be passed as a single arguments, you should quite it.

I.e. you should invoke your command this way:

Code:
program_name "word1 word2 ... wordN"
- in case of program_name being a Perl script you'll get everything, i.e. all the

word1 word2 ... wordN

in $ARGV[0].

Of course, you'll have a problem if you want '"' in your "word1", etc., but this is easily resolved by escaping the quotes, e.g.:

Code:
"\"foo\" bar"
.

Last edited by Sergei Steshenko; 11-30-2008 at 05:06 AM.
 
Old 11-30-2008, 05:30 AM   #3
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
I was going to suggest single quotes around the description:

Code:
strCommand = new String("perl " + strScriptHome + "S5a.pl '" + strDescr + "' ";
 
Old 11-30-2008, 12:52 PM   #4
Thor_2.0
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,217
Blog Entries: 23

Original Poster
Rep: Reputation: 279Reputation: 279Reputation: 279
Thanks!

I knew there was something I missed, and there it seemed to be: single quotes...

Thank you both, I'll try the suggestions right away

Grtz!

Thor
 
Old 12-04-2008, 01:37 PM   #5
Thor_2.0
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,217
Blog Entries: 23

Original Poster
Rep: Reputation: 279Reputation: 279Reputation: 279
Eh...

Nope...

Ok, this is the current scenario:

I use Java to start a perl script, and give it some params. Just as illustrated above. I do a print in the script, and in the standard out (the screen) the param comes out right, when I try to write the same param to a file, the whole thing gets chopped up in the spaces and spread over the subsequent prints...

Here's the code (if someone's interested...I am at a loss here, and never did any perl, I am learning, though...)

---------------------------------------------------------------------------
(read-outs of the args here)

chdir $folder;
open(PFH,">>P001.dat") || die "Could not write to the DAT file...";
print PFH "<px>\n";
print PFH "<dlu>$dlu</dlu>\n";
print "writing $dlu"; <-- this yields the whole string correctly
print PFH "<dam>$dam</dam>\n"; <-- part 1
print PFH "<fla>$flu</fla>\n"; <-- part 2
print PFH "<phf>$phf</phf>\n"; <-- part 3
print PFH "<hfn>$hfn</hfn>\n"; <-- part 4
print PFH "<pin>$pin</pin>\n"; <-- part ????
print PFH "</px>\n";
close(PFH);
---------------------------------------------------------------------------
As you can imagine, I write XML to the disk...
If there is some XML handler in perl, I try not to use it, this to avoid too excessive system requirements for the end-user...

Can someone explain where I went wrong, how I find the "light" and solve this problem ?

Tnx!!!

Thor
 
Old 12-04-2008, 02:39 PM   #6
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
What are you passing to Perl and which parameters are supposed to contain what?

Please give specifics.
 
Old 12-04-2008, 02:52 PM   #7
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
Additionally, please show the code where you are setting the values of:

$folder
$dlu
$dam
$flu
$phf
$hfn
$pin
 
Old 12-04-2008, 04:52 PM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by linusr@flanders View Post
Eh...

Nope...

Ok, this is the current scenario:

I use Java to start a perl script, and give it some params. Just as illustrated above. I do a print in the script, and in the standard out (the screen) the param comes out right, when I try to write the same param to a file, the whole thing gets chopped up in the spaces and spread over the subsequent prints...

Here's the code (if someone's interested...I am at a loss here, and never did any perl, I am learning, though...)

---------------------------------------------------------------------------
(read-outs of the args here)

chdir $folder;
open(PFH,">>P001.dat") || die "Could not write to the DAT file...";
print PFH "<px>\n";
print PFH "<dlu>$dlu</dlu>\n";
print "writing $dlu"; <-- this yields the whole string correctly
print PFH "<dam>$dam</dam>\n"; <-- part 1
print PFH "<fla>$flu</fla>\n"; <-- part 2
print PFH "<phf>$phf</phf>\n"; <-- part 3
print PFH "<hfn>$hfn</hfn>\n"; <-- part 4
print PFH "<pin>$pin</pin>\n"; <-- part ????
print PFH "</px>\n";
close(PFH);
---------------------------------------------------------------------------
As you can imagine, I write XML to the disk...
If there is some XML handler in perl, I try not to use it, this to avoid too excessive system requirements for the end-user...

Can someone explain where I went wrong, how I find the "light" and solve this problem ?

Tnx!!!

Thor
Regarding

Code:
open(PFH,">>P001.dat") || die "Could not write to the DAT file...";
- this is bad in any language - the end user would never be able figure out what file could not be opened.

The right idiom is more or less like this:

Code:
my $dat_file = 'P001.dat'
open(my $fh, '>>', $dat_file) or die "could not open '$dat_file' file for appending";
This does not solve you problem, but you haven't documented it properly.

You need to give full script source and the resulting file.

Have you put

Code:
use warnings;
use strict;
in the beginning of your Perl script ?

If not, first put them - you really need to know what you are doing if are not using the above pragmas.

The pragmas show you a lot of your mistakes, and the very first thing you need to do is to get rid of all the warnings and errors.
 
Old 12-04-2008, 05:45 PM   #9
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,289

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
For the open(), I'd also add $! at the end of the msg, which prints out the reason why it fails (if it does).
Code:
open(my $fh, '>>', $dat_file) or die "Can't append to $dat_file: $!\n";
 
Old 12-04-2008, 10:09 PM   #10
Thor_2.0
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,217
Blog Entries: 23

Original Poster
Rep: Reputation: 279Reputation: 279Reputation: 279
Hi,

For the people who need more specific info, this is the command I call Perl with

perl /home/devs/Str4/Scripts/S5a.pl /home/devs/Str4 "Fri Dec 05 04:53:52 CET 2008" "Fri Dec 05 04:53:52 CET 2008" 1 /home/devs/Dump "The Project" PX013

In the script these are called of as :
$folder = $ARGV[0];
$dlu = $ARGV[1];
$dam = $ARGV[2];
$flu = $ARGV[3];
$phf = $ARGV[4];
$hfn = $ARGV[5];
$pin = $ARGV[6];

in sequence that makes home folder (arg 0), date made, date last used, flag for last used, folder for the logs, a human-friendly name and the internal name (caps optional in that last one)

And this is the snippet of the output
<dlu>"Fri</dlu>
<dam>Dec</dam>
<fla>05</fla>
<phf>04:53:52</phf>
<hfn>CET</hfn>
<pin>2008"</pin>

As you see, the first param - the date - gets chopped up...

Though I check the integrity with a check-print before I print it to the file, there it is OK;..

Tnx

Thor
 
Old 12-05-2008, 01:37 AM   #11
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
Test the perl script directly from a shell prompt.

My test code:
Code:
#!/usr/bin/perl
###
### Test code
###

my ($folder,$dlu,$dam,$flu,$pfh,$hfn,$pin)=@ARGV;
my $data_file="P001.dat";

chdir $folder;
open(PFH, ">> ", $data_file) or die "Could not append to $data_file : $!\n";

print PFH "<px>\n";
print PFH "<dlu>$dlu</dlu>\n";
print "writing $dlu \n";
print PFH "<dam>$dam</dam>\n";
print PFH "<fla>$flu</fla>\n";
print PFH "<phf>$phf</phf>\n";
print PFH "<hfn>$hfn</hfn>\n";
print PFH "<pin>$pin</pin>\n";
print PFH "</px>\n";
close(PFH);
Calling:
Code:
./Testcode.pl /tmp/output "$(date)" "$(date)" 1 /home/dev/dump 1 "The Project" PX013
Creates /tmp/output/P001.dat:
Code:
<px>
<dlu>Fri Dec  5 07:31:25 GMT 2008</dlu>
<dam>Fri Dec  5 07:31:25 GMT 2008</dam>
<fla>1</fla>
<pfh>/home/dev/dump</pfh>
<hfn>The Project</hfn>
<pin>PX013</pin>
</px>
As far as I understand, this is what you are trying to achieve.

If this works from a shell prompt, then the issue is not in the perl code, but where you are calling perl.
 
Old 12-05-2008, 02:07 AM   #12
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by linusr@flanders View Post
Hi,

For the people who need more specific info, this is the command I call Perl with

perl /home/devs/Str4/Scripts/S5a.pl /home/devs/Str4 "Fri Dec 05 04:53:52 CET 2008" "Fri Dec 05 04:53:52 CET 2008" 1 /home/devs/Dump "The Project" PX013

In the script these are called of as :
$folder = $ARGV[0];
$dlu = $ARGV[1];
$dam = $ARGV[2];
$flu = $ARGV[3];
$phf = $ARGV[4];
$hfn = $ARGV[5];
$pin = $ARGV[6];

in sequence that makes home folder (arg 0), date made, date last used, flag for last used, folder for the logs, a human-friendly name and the internal name (caps optional in that last one)

And this is the snippet of the output
<dlu>"Fri</dlu>
<dam>Dec</dam>
<fla>05</fla>
<phf>04:53:52</phf>
<hfn>CET</hfn>
<pin>2008"</pin>

As you see, the first param - the date - gets chopped up...

Though I check the integrity with a check-print before I print it to the file, there it is OK;..

Tnx

Thor
It looks you haven't put

Code:
use warnings;
use strict;
-it's not worth spending time debugging scripts until the scripts are clean WRT the two pragmas.
 
Old 12-05-2008, 12:45 PM   #13
Thor_2.0
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,217
Blog Entries: 23

Original Poster
Rep: Reputation: 279Reputation: 279Reputation: 279
Ok,

So use warnings, and use strict; were active, not included in the snippet, sorry. I did a rebuild of the code and tested it, in the terminal, I got a clean XML, the program itself only made the headers.

One note (detail, really) there are three scripts, one to write the header
<?xml version='1.0' encoding='ISO-8859-1'?>
<pxList>

and one to write the endtag, these work properly, thy both bet the destination folder as tparam and that works...

The middle bit, is ... a problem...

The test I did was to force the program to dump the String to the System.out.println() each time, then I looked in the XML. I reconstructed the sequence, copying and pasting the dumped Strings from the console to the Terminal, that way I did three calls, and ended up with the proper XML.

May Java is bad...that's all...
It's quite clear my (first) attempt at Perl was ok...

If I do a call from Java, do I need to do a call in some prepared way, I'm suspecting that I dont use the proper format to do a system call...

Again, big-big-big tnx in advance 4 some input...

Thor
 
Old 12-05-2008, 01:13 PM   #14
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
Try using a combination of single and double quotes.

Code:
strCommand = new String("perl " + strScriptHome + "S5a.pl " + strFolder + '"' + strDlu +'" "' + strDam + '" ' + strFlu + " " + strPfh + " '" + strHfn + "' " + strPin);

Last edited by Disillusionist; 12-05-2008 at 01:15 PM.
 
Old 12-05-2008, 01:22 PM   #15
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by linusr@flanders View Post
...
May Java is bad...that's all...
It's quite clear my (first) attempt at Perl was ok...

If I do a call from Java, do I need to do a call in some prepared way, I'm suspecting that I dont use the proper format to do a system call...

...

Thor
You approach is bad.

A good approach (I'll write it in Perl for Perl, you'll replace the upper PErl part with Java) is to always first construct a command in a variable, then to print it (to STDERR), then to execute it.

In such a manner you always know what you are trying to execute and you always can reproduce the same conditions interactively. So, the code:

Code:
my $dir = 'foo';
my $file = 'bar.txt';
my $cmd = "/bin/ls -lt $dir/$file";
warn ":INFO: about to execute \$cmd=$cmd\n";
system($cmd);
- implement the above approach in your Java code, and you'll be happy.
 
  


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
Can u plzz help... Regarding module params... bhargav_crd Linux - Kernel 2 03-04-2008 11:34 PM
'upgradepkg' params during 10.2 -> 11.0 HaroldWho Slackware 3 11-27-2006 01:10 PM
Passing params to Lilo with out a prompt ? michael_util Linux - General 1 02-11-2005 02:19 PM
where are these params? michi Linux - General 1 09-16-2002 01:32 PM
network params ph33r Linux - Networking 5 09-03-2002 06:59 AM


All times are GMT -5. The time now is 06:24 PM.

Main Menu
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