tags:

views:

108

answers:

4

Hello,

i am calling a perl script client.pl from a main script to capture the output of client.pl in @output.

is there anyway to avoid the use of these two files so i can use the output of client.pl in main.pl itself

here is my code....

main.pl
=======

my @output = readpipe("client.pl");

client.pl
=========

#! /usr/bin/perl -w
#use strict;
use Socket;

#initialize host and port


my $host = shift || $FTP_SERVER;


my $port = shift || $CLIENT_PORT;

my $proto = getprotobyname('tcp');

#get the port address
my $iaddr = inet_aton($host);
my $paddr = sockaddr_in($port, $iaddr);

#create the socket, connect to the port
socket(SOCKET, PF_INET, SOCK_STREAM, $proto)or die "socket: $!\n";
connect(SOCKET, $paddr) or die "connect: $!\n";

my $line;
while ($line = <SOCKET>)
{
    print "$line\n";
}
close SOCKET or die "close: $!";

/rocky..

+3  A: 

Put the common code in a package. Use the package in client.pl and main.pl. Chapter 10 of Programming Perl has more information.

outis
for packages, he first needs to use subroutines.
Neeraj
A: 

Not sure what you are really trying to do, but might worh investigating a package such as Net::FTP ( http://search.cpan.org/perldoc?Net%3A%3AFTP )

justintime
A: 

you can do two things:

  1. Merge the codes in client.pl and main.pl as your main function does no work other than printing. In case you want to do more from the incoming input data, you should do that in client.pl itself, coz an in-memory array(@output) may run out of RAM while reading large size data across the network.

  2. If you want the output in an array (@output)


    sub client {
     # intialize ..
     my @array =  (); #empty array
     while ($line = <SOCKET>)
     {
      push(@array,$line);
     }
     return @array;
    }

   @output = client();

   print @output;

Other way, you can also use references:



    sub client {
     # intialize ..

     my @array =  (); #empty array
     while ($line = <SOCKET>)
     {
      push(@array,$line);
     }
     return @array;
    }

   my $output_ref = client();

   print @$output_ref; // dereference and print.
Neeraj
A: 

Hello,

i tried to avoid , a perl script calling an another perl script using the sub() in main script, but the problem is i am not getting the output of that sub routine in the array

here is the code i am using :::

i tried to call &server subroutine in main script [my @output = readpipe(&server);] but did not get any thing in the array @output rather i am getting on screen which i need for further processing (got a 1 connection from:)

could anybody suggest??

while(1)
{

print"Hello, Waiting For Connection From The Client\n";

#my @output = readpipe("server.pl");

my @output = readpipe(&server);
#my @output = readpipe(server());

print"Bye, Confirmed The Connection from Client\n";

print @output;
open FILE, ">server.txt" or die $!; 
print FILE @output; 
close FILE;

$data_file="server.txt";
open(ROCK, $data_file)|| die("Could not open file!");
@raw_data=<ROCK>; 

@COUNT_PS =split(/ /,$raw_data[1]);
my $LOOP_COUNT_PS_3 = $COUNT_PS[2];
my $value = "$LOOP_COUNT_PS_3";
print "$value";
open FILE, ">value.txt" or die $!; 
print FILE $value; 
close FILE;
close(ROCK);
sleep 5;

#if ($value)
#{
system("iperf.exe -u -c 10.232.62.151 -p 5001 -t 5 -b 3600k -i 1");
#}
}


#!/usr/bin/env perl
#use strict;
#use warnings;



sub server{

 #! /usr/bin/perl -w
 # server0.pl
 #--------------------

 #use strict;
 use Socket;

 # use port 7890 as default
 my $port = shift || 7870;
 my $proto = getprotobyname('tcp');

 # create a socket, make it reusable
 socket(SERVER, PF_INET, SOCK_STREAM, $proto) or die "socket: $!\n";
 setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "setsock: $!\n";

 # grab a port on this machine
 my $paddr = sockaddr_in($port, INADDR_ANY);
 #my $paddr = inet_aton("192.168.16.2");

 # bind to a port, then listen
 bind(SERVER, $paddr) or die "bind: $!\n";
 listen(SERVER, SOMAXCONN) or die "listen: $!\n";
 print "SERVER started on port $port \n";
 $value = 0;
 # accepting a connection

 my $client_addr;
 while ($client_addr = accept(CLIENT, SERVER))
 {
 # find out who connected
 my ($client_port, $client_ip) = sockaddr_in($client_addr);
 my $client_ipnum = inet_ntoa($client_ip);
 my $client_host = gethostbyaddr($client_ip, AF_INET);


 $count=++$value; 

 # print who has connected
 print "got a $count connection from: $client_host","[$client_ipnum] \n";
 # send them a mesage, close connection


 print CLIENT "This is $count Connection from the server\n";

 #print CLIENT "$count\n";
 close CLIENT;

 close SERVER;

} 

/rocky..

}

`readpipe EXPR` evaluates EXPR and executes the result as a `system` command, it stuffs the results into either an array or a scalar depending on context. Your `server` subroutine returns the result of the call to `close` the `SERVER` global file handle. Since `close` returns a boolean, it's unlikely to do what you want. I suggest that you read the perldoc for each function you use and pay close attention to the arguments and return of each item. You seem to suffer the misapprehension that Perl is a glorified shell language. In normal use you are unlikely to need to fork subprocesses.
daotoad