Perl WorkNote1

=================================================================
perl中获取当前系统路径

在perl的程序当中,有时候会用到当前的系统路径。

perl中获取当前李靖有两种方法:
1、使用CWD包
     use Cwd;
     print getcwd;

2、使用环境变量
     print $ENV{'PWD'};

我更倾向于使用环境变量,这样不会引入额外的包

3.使用shell命令
      print system("pwd");

=================================================================
打开文件,并往文件里面写信息,如文件不存在,则会生成。
#!/usr/bin/perl -w
open DENO, ">/home/haiouc/dailyDeno";
print DENO ("hello, world!");
close DENO;

打开指定文件,并打印出;
my $myfile;
open(myfile,"c://label.txt")||die "Cann't open it !";
while(my @content=)
     {
    print @content;
  }

=================================================================
写入文件

open(filehandle,">pathname")
open(filehandle,">>pathname")

可以同时打开多个文件句柄,以便进行读取和写入操作
open(source,"sourcefile")||die "$!";
open(dest,"destination")||die "$!";
@contents=;
print dest @contents;
close(dest);
close(source);

上面这个代码段实现了一个简单的文件拷贝。实际上同时进行读取和写入操作可以将例程缩短一些:
print dest ;
由于print函数希望有一个列表作为其参数,因此是在列表上下文中计算的。当尖括号运算符在列表上下文中进行计算时,整个文件将被读取,然后输出到文件句柄DEST。
=================================================================
在当前目录输入名称后创建一个该名称的目录,并设置目录的权限;

#!/usr/bin/perl -w
print "Directory to create?";
my $newdir=<STDIN>;        ===>> 使用行输入操作符<STDIN>
chomp $newdir;                  ===>> chomp 去除换行符(如果字符串结尾有换行符,chomp可以去掉它,这基本上就是它能完成的所有功能)    
mkdir($newdir, 0755)||die "Failed to create $newdir:$!";

=================================================================
获取当前系统时间及按照指定格式输出

#!/usr/bin/perl -w
my $date=localtime;
print "$date";       ===>>  Mon Oct  3 13:24:44 2011

============================== 

#!/usr/bin/perl -w
sub GetCurrentTime {
    my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $time) = localtime();
    return sprintf("%2.2d:%2.2d:%2.2d", $hour, $min, $sec); ===>>  12:52:56   时间格式可以根据你的需要而修改
}
$buildTime = GetCurrentTime();
print "$buildTime";  
=================================================================

猜你喜欢

转载自haiouc.iteye.com/blog/1774310
今日推荐