玩转 Perl ABC
Perl includes a rich documentation system called Perdoc,as part of the language package. Perldoc is also available on the web at perldoc.perl.org,and in fact this is probably much more convenient for most people.
下载ActivePerl 运行Komodo Edit, Command name随便取,我这里用mine "%D" (%D will run the script in the current Directory) Ctrl+Shift+R (运行Perl script) 左边存放perl文件 一个示例 说明: test.pl use warnings; #This tells the interpreter to issue warnings for potentially ambiguous or erroneous code. Perl syntax can be very loosely interpreted without this. print "Perl version is $^V"; 运行结果:(说明以上配置成功,可以使用perl了) 数组脚本举例 use 5.24.3; my $filename = "linesfile.txt"; # $表示纯变量,标量use a scalar variable for the name of the file open(FH,$filename); # open the file my $count = scalar @lines; # the number of lines in the file,scalar对数组进行操作时,会返回元素个数 linesfile.txt 循环脚本举例 use 5.24.3; my $filename = "linesfile.txt"; # the name of the file open the file - with simple error reportingmy $fh = IO::File->new( $filename,"r" ); # the object interface is stored in the $fh scalar variable. count the linesmy $count = 0; close and print$fh->close; 函数脚本举例 use 5.24.3; main(@ARGV); #@ARGV既然以@开头,标明这是一个数组。含义是包含了程序从命令行得到的所有参数。This is actually a special array that‘s predefined by Perl to contain the parameters that were passed from the command line when this script was invoked. entry pointsub main countlines ( filename ) - count the lines in a filereturns the number of linessub countlines # open the file my $fh = IO::File->new( $filename,"r" ) or error("Cannot open $filename ($!)n"); # count the lines my $count = 0; $count++ while( $fh->getline ); $fh->close; # return the result return $count; } error ( string ) - display an error message and exitsub error 复制binary files,比如windows下复制图片文件脚本举例 use warnings; my $fn1 = ‘train-station.jpg‘; my $file1 = IO::File->new("< $fn1") or die "Cannot open file: $!"; $file1->binmode; #we put them in binary mode. They default to text mode my $buffer; print "Done."; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |