首页 > Linux操作系统 > Linux操作系统 > Perl的一些重要模块使用介绍


原创 Linux操作系统 作者:物理狂人 时间:2012-04-25 11:15:14 0 删除 编辑
1) use Cwd;
my $directory = cwd;

2) use Fatal qw/ open chdir/;
chdir '/home/merlyn';  # "or die" is now supplied automatically

3) use File::Basename;
for (@ARGV){
my $basename = basename $_;
my $dirname = dirname $_;
print "That's file $basename in directory $dirname.\n";

4)use File::Copy;
copy("source", "destination")
or die "Can't copy 'source' to 'destination' : $!";

5) use File::Spec;
my $current_directory = File::Spec->curdir;
or die "Can't open current directory '$current_directory': $!";

6)use Image::Size;

#Get the size of fred.png
my($fred_height, $fred_width) = imgsize("fred.png");
die "Couldn't get the size of the image"
unless defined $fred_height;

7) use Net::SMTP;

my $from = 'YOUR_ADDRESS_GOES_HERE'; #maybe
my $site ='YOUR_SITE_NAME_GOES_HERE'; #maybe
my $smtp_host = 'YOUR_SMTP_HOST_GOES_HERE'; #maybe mail or mailhost
my $to = '';

my $smtp = Net::SMTP->new($smtp_host, Hello => $site);


$smtp->datasend("To: $to\n");
$smtp->datasend("Subject: A message from my Perl program.\n");
$smtp->datasend("This is just to let you know,\n");
$smtp->datasend("I don't care what those other people say about you,\n");
$smtp->datasend("I still think you're doing a great job.\n");
$smtp->datasend("Have you considered enacting a law naming Perl \n");
$smtp->datasend("the national programming language?\n");

$smtp->dataend(); #Not datasend!

8)use POSIX;

print "Please enter a number:";
chomp(my $str = );

$! = 0; #Clear out the error indicator
my($num, $leftover) = POSIX::strtod($str);

if($str eq ''){
print "That string was empty!\n";
my $remainder =substr $str, -$leftover;
print "The string '$remainder' was left after the number $num.\n";
print "The conversion function complained: $!\n";
print "The seemingly-valid number was $num.\n";

9)use Sys::Hostname;
my $host = hostname;
print "This machine is known as '$host'.\n";

10)use Text::Wrap;
my $message = "This is some example text which may be longer ". "than the width of your output device, so it needs to ".
"be wrapped to fit properly as a paragraph. ";

print wrap("\t", "", "$message\n");

11)use Time::Local;

my $time = timelocal($sec, $min, $hr, $day, $mon, $year);

来自 “ ITPUB博客 ” ,链接:,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录


  • 博文量
  • 访问量