Perl常用Lib

1. POSIX

use POSIX qw(strftime);

use constant NOW => strftime( '%Y%m%d.%H%M%S', localtime );

use constant TODAY => strftime( '%Y%m%d', localtime );

2. Data::Dumper

use Data::Dumper;

Dumper ( \%hash);

Dumper(\@array);

Data::Dumper->Dump( [\%hash], ["HASHDATA"] );

Data::Dumper->Dump( [\@array],["ARRAYDATA"]);

3. Getopt::EvaP

use GetOpt::Evap;

use constant ME => File::Basename::basename($0);

##############################
#Set up the options processing
#
my %opt = (); 
my (@PDT) = split (/\n/, <<"//end-of-PDT");
PDT @{[ME]}
    server,            S: string 
    db_user,         U: string = "dev"
    db_pass,        P: string = "developer"
    db_set,           D: string = "mydatabase"
    ops,                ops: string
    log_dir,           ld: file = "/tmp/test_dir/LOG"
    todo_dir,         td: file = "/tmp/test_dir/TODO"
    done_dir,       dd: file = "/tmp/test_dir/DONE"
    email,            e: string
    PDTEND optional_file_list
//end-of-PDT
;

my (@MM) = split (/\n/, <<"//end-of-MM");

@{[ME]}:  Some comments here.
    .server
    Server to connect to and apply the SQL.
    .db_user
    User name to login as.
    .db_pass
    Password for user.
    .db_set
    Destination database.

    . Name of original data file from which this SQL was generated. Name
    to be used in the mail message.
    .log_dir
    Directory where to place the generated logs.
    .todo_dir
    Directory where to take the todo sql file
    .done_dir 
    Directory where to place the processed sql file
    .email
    Email where the results will be sent.
//end-of-MM
;
EvaP(\@PDT, \@MM, \%opt)
    or die("Cannot parse arguments: \"@ARGV\" using Getopt::EvaP!");

print Dumper %opt;

4. File::Basename

use File::Basename;

use constant SCRIPT_NM => basename($0);

my $PROG = basename($0)    || die "Could not determine program name  using 'basename':$?";

my $CURRDIR =dirname($0);

my $filename = "/tmp/test_dir/test.txt";

my $dir = dirname($filename);# /tmp/test_dir

my $file = basename($filename);#test.txt

my ($base, $dir, $suffix) = fileparse($filename,'.txt');

5. File::Copy

use File::Copy;

my $src_file = "/tmp/test_dir/src.xls";

my $dst_file = "/tmp/test_dir/dst.xls";

die( "Unable to move '$src_file' to '$dst_file'.") 

unless move( $src_file, $dst_file );


useFile::Copy;

copy("file1","file2")or die "Copyfailed: $!";

copy("Copy.pm",\*STDOUT);

move("/dev1/fileA","/dev2/fileB");

 

useFile::Copy"cp";

$n = FileHandle->new("/a/file","r");

cp($n,"x")


發佈了17 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章