开发者

How To Avoid a Perl script calling an Another Perl Script

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 || 开发者_Go百科$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..


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


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 )


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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜