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 fred@bedrock.edu
my $site ='YOUR_SITE_NAME_GOES_HERE'; #maybe bedrock.edu
my $smtp_host = 'YOUR_SMTP_HOST_GOES_HERE'; #maybe mail or mailhost
my $to = 'president@whitehouse.gov';
my $smtp = Net::SMTP->new($smtp_host, Hello => $site);
$smtp->mail($from);
$smtp->to($to);
$smtp0>data();
$smtp->datasend("To: $to\n");
$smtp->datasend("Subject: A message from my Perl program.\n");
$smtp->datasend("\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("\n");
$smtp->datasend("Have you considered enacting a law naming Perl \n");
$smtp->datasend("the national programming language?\n");
$smtp->dataend(); #Not datasend!
$smtp->quit;
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";
}elsif($leftover){
my $remainder =substr $str, -$leftover;
print "The string '$remainder' was left after the number $num.\n";
}elsif($!){
print "The conversion function complained: $!\n";
}else{
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博客 ” ,链接:http://blog.itpub.net/24104518/viewspace-722192/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/24104518/viewspace-722192/
本文展示了Perl中常用模块的实际应用案例,包括文件操作、图像处理、邮件发送等,通过具体代码帮助读者理解如何使用这些模块简化开发工作。
1362

被折叠的 条评论
为什么被折叠?



