CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
redircting output of one script as input to another

 



msds
User

Sep 22, 2002, 11:47 PM

Post #1 of 16 (2650 views)
redircting output of one script as input to another Can't Post

Smile Hello, I want to combine three perl scripts into one piece of functioning code.Suppose,

Quote
a)prog1.pl reads an input string,

/does some proccessing/

produces $string1 as output, which it writes to the file : firstfile.txt

b)prog2.pl then reads $string1 as input from firstfile.txt

/does some proccessing/

produces $string2 as output, which it writes to the file : secondfile.txt

c)prog3.pl reads $string2 as input from secondfile.txt

/does some proccessing/

produces $string3 as output, which it writes to thirdfile.txt

The thirdfile.txt has the final required output.

Is there a way in which we can have a kind of "main.pl "
program, in which prog1.pl,prog2.pl,and prog3.pl can be made to execute one after the other.i.e we run the main.pl program , and check for the results in thirdfile.txt

Any help would be appreciated.
Thanx,
msdsSmile


davorg
Thaumaturge / Moderator

Sep 23, 2002, 6:02 AM

Post #2 of 16 (2638 views)
Re: [msds] redircting output of one script as input to another [In reply to] Can't Post

Well, Perl programs are just programs. You can run another Perl program just the same way as you can any other external program. In this case it sounds like "system" will do what you want.

It may be worth considering changing your programs to modules. That would make them easier to reuse.

Dave...

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Strider
Novice

Sep 25, 2002, 11:32 AM

Post #3 of 16 (2633 views)
Re: [msds] redircting output of one script as input to another [In reply to] Can't Post


Quote
I want to combine three perl scripts into one piece of functioning code.



From your description, you have three separate text files being created but only one contains the data of interest. As I read it you want to keep everything in one script and just use function calls to arrive at the final required data.

Is this conclusion wrong? Are you asking for help with function calls? Or are you looking to create a new script to control the existing ones? I'd like to help, but I am a little unclear on exactly how you intend the script to function.

David


msds
User

Sep 26, 2002, 10:21 PM

Post #4 of 16 (2625 views)
Re: [davorg] redircting output of one script as input to another [In reply to] Can't Post

Smile
Thanx Dave. I'm trying to convert prog1.pl, prog2.pl, and prog3.pl into subroutines, then call them in sequence
inside main.pl.I'm trying it with some simple code before
putting actual code into prog1.pl, prog2.pl, and prog3.pl .

Also what are the possible related skills it is helpful for a Perl programmer to have?
Thanx, msdsSmile


msds
User

Sep 26, 2002, 11:44 PM

Post #5 of 16 (2622 views)
Re: [Strider] redircting output of one script as input to another [In reply to] Can't Post

SmileHello David,


Quote
As I read it you want to keep everything in one script and just use function calls to arrive at the final required data.


That's what I'm thinking of.

Here is the code I tried:
prog1.pl:


Code
 
sub first{

$result="firstoutput";

open OUTPUT,'>op.txt' or die $!;

print OUTPUT $result;

close OUTPUT;

}


prog2.pl:


Code
 
sub second{

open OUTPUT,'op.txt' or die $!;

while($result2=<OUTPUT>)
{

open SUGGEST, '>inpsug.txt' or die $!;

print SUGGEST "\nThe soundex code of the word is:";

print SUGGEST $result2;

close OUTPUT;

close SUGGEST;

}
}


prog3.pl:


Code
sub third { 

open SUGGEST, 'inpsug.txt' or die $!;

while($finp=<SUGGEST>)
{

#print "\n$finp";

open FOUPT, '>fiop.txt' or die $!;

print FOUPT $finp;

close FOUPT;

close SUGGEST;

}

}


main.pl:


Code
require "prog1.pl"; 
require "prog1.pl";
require "prog1.pl";

do first();
do second();
do third();


On running main.pl, I get the following error message:

prog1.pl did not return a true value at C:\monisha\perlpractise\main.pl line 1.

Tool completed with exit code 255

What is the mistake I've made here?Crazy
Thanx,msdsSmile


davorg
Thaumaturge / Moderator

Sep 27, 2002, 1:30 AM

Post #6 of 16 (2620 views)
Re: [msds] redircting output of one script as input to another [In reply to] Can't Post

Any file loaded by "use" or "require" must return a true value to the calling program.

This is usually achieved by simply putting a line containing

Code
1;

at the end of the file.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Strider
Novice

Sep 27, 2002, 5:52 AM

Post #7 of 16 (2617 views)
Re: [msds] redircting output of one script as input to another [In reply to] Can't Post

Getting back to thought of combining everything into one script, you could do something similar to this:


Code
#!/usr/bin/perl  

############################################
# Restrict unsafe programming constructs
# - always good to include,
# even if not utilized at first
############################################
use strict;

############################################
# Method: first
############################################
sub first()
{
return "firstoutput";
}

############################################
# Method: second
############################################
sub second()
{
return "\nThe soundex code of the word is:";
}

############################################
# Method: third
############################################
sub third()
{
return sprintf("%s %s",
second(),
first());
}

############################################
# Main
############################################
printf("%s\n",
third());


Of course, this is just a sample and could be simplified even more depending on how you intend to use it. The main point is that, unless needed, output to a file can be avoided by using return values on the sub procedures. I may be missing the question and stating the obvious, but hopefully this helps some.

David


msds
User

Sep 29, 2002, 9:59 PM

Post #8 of 16 (2610 views)
Re: [Strider] redircting output of one script as input to another [In reply to] Can't Post

SmileThanks David,
got ur point.But actually, the string that
prog1.pl outputs(firstoutput), needs to be proccessed in
prog2.pl, which then outputs another string to prog3.pl.

prog3.pl further proccesses,and returns the reqd output string.
Hmm, but yes we can return values to subroutines instead of writing to files,even then.Then we'd have to pass one para from prog1.pl to prog2.pl, process in prog2.pl, pass the result as a para to prog3.pl.Yes , I'll try that.Thanx
msdsSmile


msds
User

Sep 29, 2002, 10:08 PM

Post #9 of 16 (2609 views)
Re: [davorg] redircting output of one script as input to another [In reply to] Can't Post

Smile Thanx Dave,

The 1; took care of the error I was getting.main.pl executes without any errors, but I'm not getting the
required output in fiop.txt.

This part is O.K:

The file op.txt gets the following written onto it:

firstoutput

The file inpsug.txt gets the following written onto it:

The code of the word is:firstoutput

The file fiop.txt gets created, but nothing gets written onto it. I need the string "firstoutput" to get written
onto it.

What could be the problem here?Unsure
Thanx, msdsSmile


Strider
Novice

Sep 30, 2002, 5:01 AM

Post #10 of 16 (2604 views)
Re: [msds] redircting output of one script as input to another [In reply to] Can't Post

Post your code, or at least that portion where you are attempting to write to fiop.txt. I'll have a better chance of answering your question if I can see exactly what is being done.

David


msds
User

Sep 30, 2002, 10:01 PM

Post #11 of 16 (2597 views)
Re: [Strider] redircting output of one script as input to another [In reply to] Can't Post

Smile Hello David, the code which I'm trying to execute
, in which nothing gets written to fiop.txt, is already
present at the beginning of the thread .But here it is again anyway.:

Code
 
prog1.pl:

sub first{
$result="firstoutput";
open OUTPUT,'>op.txt' or die $!;
print OUTPUT $result;
close OUTPUT;
}

1;

prog2.pl:

sub second {
open OUTPUT,'op.txt' or die $!;
while($result2=<OUTPUT>) {
open SUGGEST, '>inpsug.txt' or die $!;
print SUGGEST "\nThe soundex code of the word is:";
print SUGGEST $result2;
close OUTPUT;
close SUGGEST;
}
}

1;

prog3.pl:

sub third {
open SUGGEST, 'inpsug.txt' or die $!;
while($finp=<SUGGEST>) {
#print "\n$finp";
open FOUPT, '>fiop.txt' or die $!;
print FOUPT $finp;
close FOUPT;
close SUGGEST;
}
}

1;

main.pl:

require "prog1.pl";
require "prog1.pl";
require "prog1.pl";

do first();
do second();
do third();

Thanx, msdsSmile


(This post was edited by davorg on Oct 1, 2002, 1:27 AM)


davorg
Thaumaturge / Moderator

Oct 1, 2002, 1:45 AM

Post #12 of 16 (2593 views)
Re: [msds] redircting output of one script as input to another [In reply to] Can't Post

I suggest that you turn on "-w" and "use strict" in all of your Perl programming as they will help you find many problems in your code.

In this case, you have a couple of loops that look like this:

Code
open FILE, 'file' or die $!; 
while (<FILE>) {
# do something
close FILE;
}

Take a second to think what this does.

You read the first line from the file and process the line in some way. You then close the filehandle. You then go back to the start of the loop and try to read the next line from the file. Except you can't because the filehandle is now closed. "-w" would give you a warning at this point telling you that you're trying to read from a closed filehandle.

Of course, in most cases this doesn't matter to you, as your files only have one line in them, but look at the code that writes data into inpsug.txt:

Code
print SUGGEST "\nThe soundex code of the word is:";  
print SUGGEST $result2;

You're writing _two_ lines of data into the file. The first of these is empty (it ends with the newline character). Therefore when you come to read this data later on (using one of your normal, broken loops) you only read the first, empty line - and it's that which ends up being written to fiop.txt.

_Please_ learn to program with "-w" and "use strict". It will make your life (and mine) much easier :)

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


msds
User

Oct 1, 2002, 4:55 AM

Post #13 of 16 (2588 views)
Re: [davorg] redircting output of one script as input to another [In reply to] Can't Post

Smile ur the one Dave! Now the code works.
Sheesh..Blush I don't think I'm ever going
to forget about "strict" or "-w" now.
Thanx,
msds


msds
User

Oct 10, 2002, 4:52 AM

Post #14 of 16 (2576 views)
Re: [davorg] redircting output of one script as input to another [In reply to] Can't Post

Smile Hello Dave .

I tried some similar towhat we have been discussing:


Code
use strict; 
use warnings;

require "x.pl";
require "y.pl";
require "z.pl";

do one();
do two();
do three();


I get the following error message:

Use of "do" to call subroutines is deprecated at C:\soandso\appli.pl line 8.

Use of "do" to call subroutines is deprecated at C:\soandso\appli.pl line 9.

Use of "do" to call subroutines is deprecated at C:\soandso\appli.pl line 10.

Can't locate y.pl in @INC (@INC contains: D:/Perl/lib D:/Perl/site/lib .) at C:\soandso\appli.pl line 5.

Tool completed with exit code 2

What is the problem here?
Thanx,
msds


davorg
Thaumaturge / Moderator

Oct 10, 2002, 5:13 AM

Post #15 of 16 (2575 views)
Re: [msds] redircting output of one script as input to another [In reply to] Can't Post

Well, I think that Perl's error messages are generally very easy to understand. Certainly these ones mean exactly what they say.

If you ever have error messages that you don't understand, it's a good idea to add

Code
use diagnostics;

to your program. Then instead of getting one line error messages, you'll get a paragraph of useful information.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


msds
User

Oct 25, 2002, 3:47 AM

Post #16 of 16 (2551 views)
Re: [davorg] redircting output of one script as input to another [In reply to] Can't Post

Smile Thanx Dave.
Made a silly mistake like typing the name of the second file included, i.e. soundgenr.pl incorrectly.@INC had soungenr.pl in it.

Sorry for being so late in replying!

Thanx,
msdsSmile

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives