[Ilugc] Some perl tricks

  • From: girishvenkatachalam@xxxxxxxxx (Girish Venkatachalam)
  • Date: Sat, 8 Sep 2012 12:59:38 +0530

We will look at some perl tricks.

# perl -MCPAN -e 'install Foo::Bar;'

will install the module you want.

In certain OSes perl modules are distributed as packages. In such
cases you can install using
 the package management tool.

Perl comes built with several modules like Tie::File, Sys::Syslog and
so on that need not be installed
 separately.

Perl allows a lot of tricks to be performed on files, I/O, sockets and pipes.

You can read and write to a UNIX command whose output or input is
obtained from a pipe using
 the same old open() call used in perl to read or write to files.

For example,

$ cat i.pl
open F, "|netstat -rn";

while (<F>) {
        print;
}

close F;

will read the output of netstat -rn.

You could also read from an input like this:

$ cat i.pl
open F, "tr a-z A-Z|";

while (<F>) {
        print;
}

close F;

You have to type text followed by "Ctrl-D" like this.

girish is here
GIRISH IS HERE

You can read password from the terminal like this:

$ cat i.pl

 use Term::ReadPassword;
         while (1) {
           my $password = read_password('password: ');
           redo unless defined $password;
           if ($password eq 'flubber') {
             print "Access granted.\n";
             last;
           } else {
             print "Access denied.\n";
             redo;
           }
         }

You can learn a lot of nice things you can do on UNIX just by learning
Perl and reading
 the man pages of various CPAN modules.

Which have lot of code samples for you to try.

-Girish

-- 
Gayatri Hitech
http://gayatri-hitech.com

Other related posts:

  • » [Ilugc] Some perl tricks - Girish Venkatachalam