Perl的一些重要模組使用介紹

物理狂人發表於2012-04-25
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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章