some tech tips ...
[Home] [Techpad] [Tech Corner] [Frequent Interview Questions] [For Freshers]
Perl plugin for Eclipse:
Get it from here http://e-p-i-c.sourceforge.net
Removing a file which is more that x days old:
foreach (glob("*")) {
$temp=$_;
if (( -d $temp) and ($temp =~ m/[0-9]{6}\.(.*)/))
{
$age = -M ;
if ($age > 7) {
print "Removing the file $temp as it more than 7 days old\n";
system("rm -rf $temp");
} else {
print "Keeping the file $temp as it is just",int($age)," days old\n";
}
}
}
Perl to get HTTP authentication:
use LWP::UserAgent;
$ua = LWP::UserAgent->new;
$ua->credentials('site:80', 'realm', username' => 'password'); //site is just machine name eg.hostA.domain.com
my $req = HTTP::Request->new(GET => 'http://test_Authntication/url/html'); //pass the comple URL here
$ua->request($req); print $ua->content;
Perl to get all the contents of a file into a string:
open(HND,"<file_to_open.txt") or die "$1 can't be opened";$whole_string=do{ local $/; <HND> };
close HND;
Perl to automate FTP :
open(SHELL,">get_remote_file.sh");
print SHELL "#!/bin/csh\n";
print SHELL "ftp -i -n \<\< END_FTP\n";
print SHELL "open host-a.domain.com\n";
print SHELL "user user password\n";
print SHELL "cd \/tmp\n";
print SHELL "lcd \/tmp\n";
print SHELL "mget \*\.zip\n";
print SHELL "bye\n";
print SHELL "END_FTP\n";
close SHELL;
chmod 0777,"get_remote_file.sh";
system("\.\/get_remote_file.sh > get_remote_file.log");
Perl to covert IP address -> hostname and vice-versa:
=====================================
Usage: perl filename.pl <IP address or a hostname>
=====================================
use Socket;
use strict;
my $host_name = hostname($ARGV[0]);
print "$ARGV[0] resolves to $host_name\n";
sub hostname
{
my (@bytes, @octets,$packedaddr,$raw_addr,$host_name,$ip);
if($_[0] =~ /[a-zA-Z]/g)
{
$raw_addr = (gethostbyname($_[0]))[4];
@octets = unpack("C4", $raw_addr);
$host_name = join(".", @octets);
}
else
{
@bytes = split(/\./, $_[0]);
$packedaddr = pack("C4",@bytes);
$host_name = (gethostbyaddr($packedaddr, 2))[0];
}
return($host_name);
}
Perl to do simple HTTP post:
use HTTP::Request::Common qw(POST);
use LWP::UserAgent;
$ua = LWP::UserAgent->new;
my $req = POST 'http://localhost:80/hello.jsp',[ newName => 'xyz', errors => 0 ];
print $ua->request($req)->as_string;
Client/Server Socket programming in Perl:
server.pl:
use strict;
use Socket;
# use port 7890 as default
my $port = shift || 7890;
my $proto = getprotobyname('tcp');
# create a socket, make it reusable
socket(SERVER, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";
setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "setsock: $!";
# grab a port on this machine
my $paddr = sockaddr_in($port, INADDR_ANY);
# bind to a port, then listen
bind(SERVER, $paddr) or die "bind: $!";
listen(SERVER, SOMAXCONN) or die "listen: $!";
print "SERVER started on port $port ";
# 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);
# print who has connected
print "got a connection from: $client_host","[$client_ipnum] ";
# send them a message, close connection
print CLIENT "Smile from the server";
close CLIENT;
}
client.pl:
#----------------
use strict;
use Socket;
# initialize host and port
my $host = shift || 'server-host.us.oracle.com';
my $port = shift || 7890;
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: $!
connect(SOCKET, $paddr) or die "connect: $!";
my $line;
while ($line=<SOCKET>)
{
print $line;
}
close SOCKET or die "close:$!";
Perform the following steps:-
Now ,start the server in ur server machine where you have written server.pl
server-host> perl server.pl
Now u can run client.pl from any machine in ur network,
client-host>perl client.pl
Smile from the server
Recursive search for a particular file , given a top level directory:
#!/bin/perl
use File::Find;
@ARGV = ('C:/topleveldir') unless @ARGV;
find(\&callup,@ARGV );
sub callup {
if($_ =~ /^I_m_looking_for_this_file.txt$/)
{
print $File::Find::name;
}
}
This module makes a file look like a Perl array, each array element corresponds to a line of the file
#!/usr/bin/perl
use Tie::File;
#-- modify all ocurrences of 'HowTo' to 'how to'
tie @lines, 'Tie::File', "readme.txt" or die "Can't read file: $!\n";
foreach ( @lines )
{
s/HowTo/how to/g;
}
untie @lines;
Converting Perl programs to exe 's:
http://www.indigostar.com/perl2exe.htm tool can serve the purpose. Exe's can be built from Perl programs and run across anywhere, without having perl interpreter