Help - Search - Member List - Calendar
Full Version: FAQ
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
Support our Sponsors!
Madhurima Das
Kindly tell the pseudocode to solve the following
problem:

An user input is provided to a perl program by
STDIN. This input is utilised by a fortran program to
perform some function. How do i connect both the
programs & get the output from the fortran program to
the user via the perl program itself.
Waiting for an early response. thanking you...




_______________________________________________________
Too much spam in your inbox? Yahoo! Mail gives you the best spam protection for FREE! http://in.mail.yahoo.com

Chris Devers
On Wed, 6 Jul 2005, madhurima das wrote:

QUOTE
Kindly tell the pseudocode to solve the following
problem:

An user input is provided to a perl program by
STDIN. This input is utilised by a fortran program to
perform some function. How do i connect both the
programs & get the output from the fortran program to
the user via the perl program itself.
Waiting for an early response. thanking you...

Here's some pseudocode:

$response = get_user_input();
$result = have_fortran_work_on( $response );
$finish = tell_user_about( $result );
# maybe or maybe not do anything with $finish

For the Fortran bit I've stubbed into have_fortran_work_on(), the usual
way to do this, short of an Inline::Fortran module on CPAN (there
doesn't seem to be one, though there are ones like Inline::C, etc), is
to make a system() call to the external program.

<http://perldoc.perl.org/functions/system.html>

Try using a system() call in your own pseudocode for
have_fortran_work_on().

If you're stuck, ask more specific questions, and show what you've tried
so far.



--
Chris Devers

Thomas Btzler
madhurima das <[Email Removed]> asked:
QUOTE
An user input is provided to a perl program by STDIN. This
input is utilised by a fortran program to perform some
function. How do i connect both the programs & get the output
from the fortran program to the user via the perl program itself.
Waiting for an early response. thanking you...

The section "Bidirectional Communication with Another Process"
in the perlipc manpage ("perldoc perlipc") should tell you all
you need to know (and then some ;-)).

HTH,
Thomas

Jay Savage
On 7/6/05, Chris Devers <[Email Removed]> wrote:
QUOTE
On Wed, 6 Jul 2005, madhurima das wrote:

Kindly tell the pseudocode to solve the following
problem:

An user input is provided to a perl program by
STDIN. This input is utilised by a fortran program to
perform some function. How do i connect both the
programs & get the output from the fortran program to
the user via the perl program itself.
Waiting for an early response. thanking you...

Here's some pseudocode:

$response = get_user_input();
$result  = have_fortran_work_on( $response );
$finish  = tell_user_about( $result );
# maybe or maybe not do anything with $finish

For the Fortran bit I've stubbed into have_fortran_work_on(), the usual
way to do this, short of an Inline::Fortran module on CPAN (there
doesn't seem to be one, though there are ones like Inline::C, etc), is
to make a system() call to the external program.

<http://perldoc.perl.org/functions/system.html

Try using a system() call in your own pseudocode for
have_fortran_work_on().

If you're stuck, ask more specific questions, and show what you've tried
so far.



--
Chris Devers

system() probably won't do what you want: it's value it the return
value of the wait system call, not the output of the external program.
If you want to capture the output directly (as opposed to, say,
writing to a file and then parsing the file in Perl) you'll want to
use backticks or qx//. See 'perldoc -f system' or '`STRING`' or
'qx/STRING/' in perlop for more info.

HTH

-- jay
--------------------------------------------------
This email and attachment(s): [ ] blogable; [ x ] ask first; [ ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com http://www.dpguru.com http://www.engatiki.org

Madhurima Das
Thanks for the reply sir. But i am still stuck with
the same old problem since the system() function isn't
working & the manpages aren't of much help either.

Giving below an account of what my programs are &
their output:

####PERL PROGRAM####
#assign.pl#
#!/usr/bin/perl
use strict;
use warnings;
print "enter the first numbern";
my $x = <STDIN>;
chomp $x;
print "enter the second numbern";
my $y = <STDIN>;
chomp $y;
my $z = system("assign.f",'$x','$y');
print "Output of the two numbers is:n";
print $z,"n";
exit;

#output#
QUOTE
$perl assign.pl
enter the first number

2
enter the second number
4
Can't exec "assign.f": Permission denied at assign
line 10, <STDIN> line 2.
Output of the two numbers is:
-1

#####FORTRAN PROGRAM#####
c assign.f
WRITE(*,*) "ENTER FIRST NUMBER"
READ(*,*) x
WRITE(*,*) "ENTER SECOND NUMBER"
READ(*,*) y
z = x+y
WRITE(*,*)z
END

#output#
QUOTE
$g77 assign.f
$ ./a.out
ENTER FIRST NUMBER

2
ENTER SECOND NUMBER
4
OUTPUT OF THE TWO NUMBERS IS:
6.


Can you suggest sir what should be my approach to this
problem. Waiting for an early response from you.
Thanking you..





_______________________________________________________
Too much spam in your inbox? Yahoo! Mail gives you the best spam protection for FREE! http://in.mail.yahoo.com

Madhurima Das
Thanks for the reply sir. But i am still stuck with
the same old problem since the system() function isn't
working & the manpages aren't of much help either.

Giving below an account of what my programs are &
their output:

####PERL PROGRAM####
#assign.pl#
#!/usr/bin/perl
use strict;
use warnings;
print "enter the first numbern";
my $x = <STDIN>;
chomp $x;
print "enter the second numbern";
my $y = <STDIN>;
chomp $y;
my $z = system("assign.f",'$x','$y');
print "Output of the two numbers is:n";
print $z,"n";
exit;

#output#
QUOTE
$perl assign.pl
enter the first number

2
enter the second number
4
Can't exec "assign.f": Permission denied at assign
line 10, <STDIN> line 2.
Output of the two numbers is:
-1

#####FORTRAN PROGRAM#####
c assign.f
WRITE(*,*) "ENTER FIRST NUMBER"
READ(*,*) x
WRITE(*,*) "ENTER SECOND NUMBER"
READ(*,*) y
z = x+y
WRITE(*,*)z
END

#output#
QUOTE
$g77 assign.f
$ ./a.out
ENTER FIRST NUMBER

2
ENTER SECOND NUMBER
4
OUTPUT OF THE TWO NUMBERS IS:
6.


Can you suggest sir what should be my approach to this
problem. Waiting for an early response from you.
Thanking you..





__________________________________________________________
How much free photo storage do you get? Store your friends 'n family snaps for FREE with Yahoo! Photos http://in.photos.yahoo.com

Madhurima Das
Thanks for the reply sir. But i am still stuck with
the same old problem since the system() function isn't
working & the manpages aren't of much help either.

Giving below an account of what my programs are &
their output:

####PERL PROGRAM####
#assign.pl#
#!/usr/bin/perl
use strict;
use warnings;
print "enter the first numbern";
my $x = <STDIN>;
chomp $x;
print "enter the second numbern";
my $y = <STDIN>;
chomp $y;
my $z = system("assign.f",'$x','$y');
print "Output of the two numbers is:n";
print $z,"n";
exit;

#output#
QUOTE
$perl assign.pl
enter the first number

2
enter the second number
4
Can't exec "assign.f": Permission denied at assign
line 10, <STDIN> line 2.
Output of the two numbers is:
-1

#####FORTRAN PROGRAM#####
c assign.f
WRITE(*,*) "ENTER FIRST NUMBER"
READ(*,*) x
WRITE(*,*) "ENTER SECOND NUMBER"
READ(*,*) y
z = x+y
WRITE(*,*)z
END

#output#
QUOTE
$g77 assign.f
$ ./a.out
ENTER FIRST NUMBER

2
ENTER SECOND NUMBER
4
OUTPUT OF THE TWO NUMBERS IS:
6.


Can you suggest sir what should be my approach to this
problem. Waiting for an early response from you.
Thanking you..





__________________________________________________________
How much free photo storage do you get? Store your friends 'n family snaps for FREE with Yahoo! Photos http://in.photos.yahoo.com

Thomas Btzler
madhurima das <[Email Removed]> asked:
QUOTE
Thanks for the reply sir. But i am still stuck with the same
old problem since the system() function isn't working & the
manpages aren't of much help either.

system() won't work the way you want to use it. From the manpage:
"The return value is the exit status of the program as returned
by the wait call."

Passing the arguments the way you do also doesn't work because
your Fortran code expects to read them from standard input.

This looks like a job for open2 or the expect module.

HTH,
Thomas

Jeff 'japhy' Pinyan
On Jul 7, madhurima das said:

QUOTE
#assign.pl#
#!/usr/bin/perl
use strict;
use warnings;
print "enter the first numbern";
my $x = <STDIN>;
chomp $x;
print "enter the second numbern";
my $y = <STDIN>;
chomp $y;
my $z = system("assign.f",'$x','$y');

Many things wrong here:

1. system() does NOT return the OUTPUT of the program it runs (see the
docs: perldoc -f system)
2. you have single quoted $x and $y, which means they are simply the
strings $x and $y -- you wanted Perl's values for those variables
3. you're calling assign.f, instead of a.out
4. assign.f reads its values from STDIN, not from the commandline

Assuming you have compiled assign.f to a.out, I would do the following:

use IPC::Open2;
open2 my($read), my($write), "./a.out"
or die "can't open2 ./a.out: $!";

<$read>; # read (and discard) a line of output
print $write "$xn";

<$read>; # read (and discard) the next line of output
print $write "$yn";

chomp(my $z = <$read>); # read (and save) the next line of output

close $read;
close $write;

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart

Muthukumar
Can you try with backtick or qx[ ] ?

my $z = `assign.f $x $y`;

hth.

QUOTE
Can't exec "assign.f": Permission denied at assign
line 10, <STDIN> line 2.
Output of the two numbers is:
-1

--Muthu


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2005 Invision Power Services, Inc.