Use of common Perl modules

Source: Internet
Author: User
Tags date1

A brief description of http://www.perldoc.com/perl5.6/pod/perlmodlib.html for some common modules

Use modules in Perl:
Module: http://www.cpan.org/modules/01modules.index.html
Installation module:
1. Perl makefile. pl
2. Make
3. Make Test
4. Make install
You can also run the following command to install the module (the applicable system RedHat 9.0 is known. I don't know about other systems. Please try again.
Perl-mcpan-e shell>

Enter: Install model_name

View the module help:
Perldoc model_name
For example:
Perldoc net: ftp

Existing modules: (the following content is transferred from CU. Thank you for your friends)
Note:
The following code is tested in FreeBSD & Solaris. The Perl version is 5.005_03.

(1) Net: ftp
(2) Net: Telnet
(3) lwp: simple, get ()
(4) Secondary CT
(5) XML: simple, xmlin ()
(6) data: dumper, dumper ()
(7) IO: Socket
(8) Date: manip, datec1c (), unixdate ()
(9) Date: manip, date_cmp ()
(10) file: Find, find ()
(11) extutils: installed, new (), modules (), version ()
(12) DBI, connect (), prepare (), execute (), fetchrow_array ()
(13) getopt: std
(14) proc: processtable
(15) Shell
(16) Time: hires, sleep (), time ()
(17) HTML: linkextor, links (), parse_file ()
(18) Net: Telnet, open (), print (), Getline ()
(19) compress: zlib, gzopen (), gzreadline (), gzclose ()
(20) Net: POP3, login (), list (), get ()
(21) Term: ansicolor
(22) Date: calc calendar (), today ()
(23) Term: CAP, tgetend (), tgoto, tputs ()
(24) HTTPD: Log: Filter
(25) Net: LDAP
(26) Net: SMTP mail (), to (), data (), datasend (), auth ()
(27) MIME: base64, encode_base64 (), decode_base64 ()
(28) Net: IMAP: simple, login (), mailboxes (), select (), get ()...
(29) Bio: DB: NOC, Bio: seqio
(30) Spreadsheet: parseexcel
(31) Text: csv_xs, parse (), fields (), error_input ()
(32) Benchmark

Note:
The code below is tested under RH linux7.2, And the Perl version is 5.6.0.

(33) http: daemon, accept (), get_request ()...
(34) array: Compare, compare (), full_compare ()...
(35) algorithm: diff, diff ()
(36) List: util, max (), min (), sum (), maxstr (), minstr ()...
(37) HTML: parser
(38) Mail: sender
(39) Time: hires, gettimeofday (), usleep ()
(40) image: magick

The following modules pass through RedHat 9.0 and Perl version v5.8.0 built.
(41) data: searchreplace
----------------------------------------------------------
(1) Net: ftp

#! /Usr/bin/perl-W
# File: ftp_recent.pl
# Figure 6.1: downloading a single file with net: ftp
Use Net: FTP;

Use constant host => 'ftp .perl.org ';
Use constant dir => '/pub/CPAN ';
Use constant file => 'recent ';

My $ FTP = net: ftp-> New (host) or die "couldn't connect :";
$ FTP-> login ('anonymous') or die $ FTP-> message;
$ FTP-> CWD (DIR) or die $ FTP-> message;
$ FTP-> get (File) or die $ FTP-> message;
$ FTP-> quit;

Warn "file retrieved successfully. \ n ";

-----------------------------------------------------------
(2) Net: Telnet
#! /Usr/bin/perl-W
# File: remoteps. pl

Use strict;
Use Net: Telnet;
Use constant host => 'phage .cshl.org ';
Use constant user => 'lstein ';
Use constant pass => 'xyting ';

My $ Telnet = net: telnet-> New (host );
$ Telnet-> login (user, pass );
My @ lines = $ telnet-> cmd ('ps-Ef ');
Print @ lines;

--------------------------------------------------------------
(3) lwp: simple, get ()
#! /Usr/bin/perl-W
Use strict;
Use lwp: simple QW (get );

My $ url = Shift | "http://www.chinaunix.net ";
My $ content = get ($ URL );

Print $ content;

Exit 0;
The simplest and most convenient way to get a webpage.

-------------------------------------------------------------
(4) Secondary CT

PHP code:

#! /Usr/bin/perl
Use strict;
Use reverse CT;

My $ timeout = 2;
My $ delay = 1;
My $ cmd = "ssh ";
My @ Params = QW/202.108.xx.xx-lusername-P22 /;
My $ pass = "passwd ";

My $ exp = benchmark CT-> spawn ($ cmd, @ Params) or die "can't spawn $ cmd \ n ";
$ Exp-> round CT ($ timeout,-Re => '[PP] assword :');
$ Exp-> send_slow ($ delay, "$ Pass \ r \ n ");

$ Exp-> interact ();
$ Exp-> hard_close ();

Exit 0;

-----------------------------------------------------------------
5) XML: simple, xmlin ()

PHP code:

#! /Usr/bin/perl-W
Use strict;
Use XML: simple;
My $ text = <XML;
<? XML version = "1.0 "? >
<Web-app>
<Servlet>
<Servlet-Name> php </servlet-Name>
<Servlet-class> net. php. servlet </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> php </servlet-Name>
<URL-pattern> *. php </url-pattern>
</Servlet-mapping>
</Web-app>
XML
My $ x = xmlin ($ text );
Foreach my $ tag (Keys % $ X)
{
My % H =%{$ $ X {$ tag }};
Foreach (Keys % H)
{
Print "$ tag => ";
Print "$ _ => $ H {$ _} \ n ";
}
}
Exit 0;

----------------------------------------------------------------
(6) data: dumper, dumper ()

PHP code:

#! /Usr/bin/perl-W
Use strict;
Use Data: dumper;

Print dumper (@ INC );
Print dumper (% env );
Exit 0;

-------------------------------------
(7) IO: Socket

PHP code:

#! /Usr/bin/perl-W
Use strict;
Use IO: socket;

My $ host = "www.chinaunix.net ";
My $ Port = "80 ";
My $ http_head = "Get/HTTP/1.0 \ nhost: $ HOST: $ port \ n ";
My $ sock = IO: Socket: iNet-> New ("$ HOST: $ port ")
Or die "socket () error, reason: $! \ N ";

Print $ sock $ http_head;
Print <$ sock>;

Exit 0;

---------------------------------------------------------------
(8) Date: manip, datec1c (), unixdate ()

PHP code:

#! /Usr/bin/perl
Use strict;
Use Date: manip;
My $ date1 = "Fri Jun 6 18:31:42 GMT 2003 ";
My $ date2 = "2003/05/06 ";
My $ flag = & date_cmp ($ date1, $ date2 );

If ($ flag <0)
{
Print "date1 is earlier! \ N ";
}
Elsif ($ flag = 0)
{
Print "the two dates are identical! \ N ";
}
Else
{
Print "date2 is earlier! \ N ";
}
Exit 0;

--------------------------------------------------------------------
10) file: Find, find ()
PHP code:

#! /Usr/bin/perl-W
Use strict;
Use File: Find;

My $ file = "access. log ";
My $ Path = "/";

Find (& process, $ PATH );

Sub process {print $ file: Find: Dir, "$ _ \ n" If (/$ FILE /);}

Exit 0;

# Search for objects in the Unix File tree structure.

---------------------------------------------------------------
(11) extutils: installed, new (), modules (), version ()

View the information of the installed modules.
PHP code:

#! /Usr/bin/perl
Use strict;
Use extutils: installed;

My $ inst = extutils: installed-> New ();
My @ modules = $ inst-> modules ();

Foreach (@ modules)
{
My $ ver = $ inst-> Version ($ _) | "??? ";
Printf ("%-12 s -- % s \ n", $ _, $ ver );
}
Exit 0;

--------------------------------------------------------------------
(12) DBI, connect (), prepare (), execute (), fetchrow_array ()

PHP code:

#! /Usr/bin/perl
Use strict;
Use dBi;

My $ DBH = DBI-> connect ("DBI: mysql: dbname", 'user', 'passwd ','')
Or die "can't connect! \ N ";
My $ SQL = QQ/show variables /;
My $ something = $ DBH-> prepare ($ SQL );
$ Something-> execute ();

While (my @ array = $ th-> fetchrow_array ())
{
Printf ("%-35 s", $ _) foreach (@ array );
Print "\ n ";
}
$ DBH-> disconnect ();
Exit 0;

------------------------------------------------------------------------
(13) getopt: std

Command Line Parameter Parsing.

PHP code:

#! /Usr/bin/perl
Use strict;
Use getopt: STD;

My % opts;
Getopts ("C: HV", % opts );

Foreach (Keys % opts)
{
/C/& print "Welcome to", $ opts {$ _} | "chinaunix ","! \ N ";
/H/& print "Usage: $0-[HV]-[c msg] \ n ";
/V/& print "this is demo, version 0.001.001 built for $ ^ O \ n ";
}
Exit 0;

------------------------------------------------------------------------
(14) proc: processtable

# Directly access the Unix Process Table, similar to ps command.

PHP code:

#! /Usr/bin/perl
Use strict;
Use proc: processtable;

My $ Pt = new proc: processtable;

Foreach (reverse sort @ {$ Pt-> table })
{
Print $ _-> PID, "=> ";
Print $ _-> cmndline, "\ n ";
}
Exit 0;

--------------------------------------------------------------------
(15) Shell

PHP code:

#! /Usr/bin/perl
Use strict;
Use shell;

Print "now is:", date ();
Print "current time is:", date ("+ % t ");

My @ dirs = ls ("-LAF ");
Foreach (@ dirs)
{
Print if (// $/); # print directory
}
Exit 0;

Shell commands are called directly as functions in Perl.

---------------------------------------------------------------------
Another use of time: hires module.

(16) Time: hires, sleep (), time ()

PHP code:

#! /Usr/bin/perl
Use strict;
Use Time: hires QW (sleep time );

$ | = 1;
My $ before = time;
For my $ I (1 .. 100)
{
Print "$ I \ n ";
Sleep (0.01 );
}
Printf ("time used: %. 5f seconds \ n", time-$ before );
Exit 0;

Use Time: hires, this module provides enhanced versions of sleep (), alarm (), time ()
Replace the built-in functions of Perl.
The sleep () and alarm () parameters can be decimal places. For example, sleep (0.1) indicates sleep for 0.1 seconds,
Time () can return floating point numbers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.