Teen Programmers Unite  
 

 

Return to forum top

Perl's Open command

Posted by Perl [send private reply] at February 13, 2003, 01:59:57 AM

Hi..i need help on this area...
Here is the case:

application 1 will run application 2 (2 separate files)and how do i pass the result of application 2 to application 1..????

sorry buzgub..i did study on the open command..i know open can run the file:

open(INFO,"perl $command|") || die("can't open frame.pl: $!");

but after i had successfully run the 2nd application...my results were displayed at the application 1....How am i going to get the results back to 1st application????

Posted by Perl [send private reply] at February 13, 2003, 02:17:31 AM

ok ignore my first post!!! i mis-interpret it

Here is the new case:
application 1 will run application 2 (2 separate files)
i have one line of code that is

APPLICATION 1:
open(INFO,"perl $command|") || die("can't open frame.pl: $!");
it will run the 2nd application

APPLICATION 2:
print"hehehe\n";print"hahaha\n";
When the 1st application run the second, it will display the back the 'hehehe' to the 1st application...but not the 'hahaha' i know due to the '\n'...
but is there any more effective ways of passing results of 2nd application back to 1st application?????

What happen if the result pass back is not just like 'hehehe' such a small sentence of words but huge amount??? can it works???

Thanks you!!

sorry buzgub..i did study on the open command..i know it can run the file!!

Posted by taubz [send private reply] at February 13, 2003, 08:51:19 AM

Everything you print in application 2 is available for application 1 to read.

Posted by Perl [send private reply] at February 13, 2003, 08:58:58 AM

serious??? i only managed to get 'hehehe' out but not the 'hahaha' out???

but when i remove the '\n'

LIKE THIS:
print"hehehe";print"hahaha\n";
it works for both....

errr...how???? i will check again and come back ..thanks

Posted by CViper [send private reply] at February 13, 2003, 02:13:19 PM

dunno about perl, but did you try reading twice (one time for the "hehehe" and one time for the "hahaha")? My guess is that you read a line each time you call your input function...

Posted by Perl [send private reply] at February 13, 2003, 08:16:19 PM

Thnks CViper...managed to solve the problem..i did a while loop to print out everything:

if(open(INFO,"perl $command|") || die("can't open frame.pl: $!")){
$wc=<INFO>;
while($wc ne "")
{
  print $cl "$wc\n";
  $wc=<INFO>;
}
  #system("perl frame.pl");
}


but dun know if it can handle large amount of data or not??still testing...

but does anyone know any links that prove good information on networking programming...esp on how to call any programs???
i tried searching....but most of them just give information on basic networking programming...thank you!~~!!
Posted by buzgub [send private reply] at February 13, 2003, 11:03:55 PM

What you did in your last post is the standard perl idiom for reading files in perl - it handles big files fine. The only catch is that if you're using it for really large files, you should make sure you write your program in such a way that it never needs to have the entire file in memory at one time.

Posted by Perl [send private reply] at February 14, 2003, 01:57:25 AM

any recommended links??

Posted by buzgub [send private reply] at February 14, 2003, 05:03:51 AM

Oh dear. I should read code before I say it's correct.

The general "Perl Way" to read a file is this (a modification of your code):

open(INFO,"perl $command|") || die("can't open frame.pl: $!"))
while ($wc=<INFO>)
{
  if ($wc ne ""){
    print "$wc\n";
  }
}
#system("perl frame.pl");


That will just run $command and print it's output to your screen - I'm not sure if it will insert lots of blank lines or not. You'll obviously want to do some other processing on it, though, but that shouldn't be too hard.
Posted by taubz [send private reply] at February 14, 2003, 02:09:06 PM

The < > operator does not chop off newlines (for that, use "chop"), so the \n is, as you thought, unnecessary.

- taubz

Posted by Perl [send private reply] at February 16, 2003, 07:14:45 PM

hmmmm....i tried both and it works and the different is how you arrange the WHILE and IF only and yr coding length is shorter than mine.

print $cl "$wc\n";--$cl is actually a client socket. i need to bind the value of $wc and send it back.

i think the key point is the CHOP...i tried printing more than one line...but it ends up printing the first line...
i think i need to use the chop to get rid of the "\n";

THANK YOU.!!~~!!

Posted by Perl [send private reply] at February 16, 2003, 07:30:32 PM

hmmm...one more question...

beside all the open stuff to activate the another program and read the results...

Is there any other method???? to achieve this???

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.