Thursday, July 14, 2011

Moudule which solve the real problem.

Net::FTPSSL


object: ftp over ssl.
server: filezilla

client: tried with ubuntu ftp-ssl. when issue ls, data channel got closed due to some version conflict. perl Net::FTPSSL moudule helps.

=============
use Net::FTPSSL;

my $ftps = Net::FTPSSL->new('172.18.9.16',
Port => 21,
Encryption => EXP_CRYPT,
Debug => 1)
or die "Can't open 172.18.9.16";

$ftps->login('aaa', 'bbb')
or die "Can't login: ", $ftps->last_message();

$ftps->cwd("/pub") or die "Can't change directory: " . $ftps->last_message();

$ftps->get("abc.txt") or die "Can't get file: " . $ftps->last_message();

$ftps->quit();


=====================
object: print the first line of file abc.txt.
io.pl abc.txt
use IO::File;

my $file=shift; #take the first para as the file name we are going to open.
my $fh=IO::File->new($file);
my $line=<$fh>; #use <> operator to read a line.
print $line;


======================

object: connect to the remote server and print the first line of the reply

io_socket.pl ftp.abc.com:ftp

use IO::Socket;
my $server=shift; #save the cli parameter to be one varible
my $fh=IO::Socket::INET->new($server);
my $line=<$fh>;
print $line;


=========================
object: digest default FileHandle: STDIN, STDOUT and STDERR

$input = ^STDIN^; #^^, angel bracket

chomp($input);

print STDOUT "If I heard you correctly, you said: $input\n";


=======================
object: open a non-existing file and print 2 lines in it.

open (FH,">ddd.txt") or die "Can't open file: $!"; #

print FH "This is the first line.\n";

print FH "And this is the second.\n";

close (FH) or die "Can't close file: $!";


Note:
< Open file for reading
> Truncate file to zero length and open for writing
>> Open file for appending, do not truncate
+> Truncate file and then open for read/write
<+ Open file for read/write, do not truncate

====================================

Object: count the line's number in a file:

use strict;
use IO::File;
my $file=shift;
my $counter=0;
my $fh=IO::File->new($file) or die "can not open $file:$!\n";

while (defined (my $line=$fh->getline)) {
$counter++;
}
STDOUT->print ("Counted $counter lines \n");

No comments: