=================================================================
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";
=================================================================
相关推荐
Interview: Rasmus Lerdorf—The Open Source Movement and Work Life 280 6.7 Record Types 284 6.8 Union Types 288 6.9 Pointer and Reference Types 292 History Note 296 Summary • Bibliographic ...
You are given 7 test cases to work with, plus 1 extra for testing a global list. You can type "debugmalloc -t n" to run the n-th test. You can see the code that is being run in driver.c. If you have ...
1:外文原文 Struts——an open-source MVC implementation This article introduces Struts, a Model-View-Controller implementation that uses servlets and JavaServer Pages (JSP) technology. Struts can help...
Changes in 2.4.6 (February 22, 2011): Brief summary : - Support more host OS to run on: - Include win64 native binary in the ... [2914433] makesym.perl misses symbols by John R. Jackson [2908481] USB ...
ASP extensions) Microsoft Visual Basic 4.x/5.x/6.x Microsoft Word, Access, Excel 95/97 (VBA) Windows Scripting Host (cscript / wscript) Perl 5.x for Win32 Inprise Delphi 4 ...