下载ActivePerl
https://www.activestate.com/products/activeperl/downloads/
下载Komodo Edit is free, Komodo IDE is not free
https://www.activestate.com/products/komodo-ide/downloads/edit/
运行Komodo Edit,
右边空白处Add, New command
Command name随便取,我这里用mine
C:\Perl64\bin\perl.exe "%F" (%F will have Perl run the script that is currently open for editing)
"%D" (%D will run the script in the current Directory)
Ctrl+Shift+R (运行Perl script)
左边存放perl文件
一个示例
#!/usr/bin/perl
print "This is my first perl program\n";
$a=<>;
print $a;
说明:
第一行: #!/usr/bin/perl 由什么程序执行以下的内容
注释:#
输入:<>
输出:print
test.pl
#!/usr/bin/perl
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了)
hello v5.24.3
数组脚本举例
#!/usr/bin/perl
use 5.24.3;
use warnings;
my $filename = "linesfile.txt"; # $表示纯变量,标量use a scalar variable for the name of the file
open(FH, $filename); # open the file
my @lines = <FH>; # read the file,全部读到数组内存
close(FH); # close the file
my $count = scalar @lines; # the number of lines in the file,scalar对数组进行操作时,会返回元素个数
print "There are $count lines in $filename";
linesfile.txt
01 This is a line of text.
02 This is a line of text.
03 This is a line of text.
循环脚本举例
#!/usr/bin/perl
use 5.24.3;
use warnings;
use IO::File;
my $filename = "linesfile.txt"; # the name of the file
my $fh = IO::File->new( $filename, "r" ); # the object interface is stored in the $fh scalar variable.
if(! $fh) { #Essentially, things that are empty or evaluate to zero tend to evaluate false, while things that are not empty or not zero tend to be true.
print("Cannot open $filename ($!)\n"); # $!根据上下文内容返回错信息
exit;
}
my $count = 0;
while( $fh->getline ) {
$count++;
}
$fh->close;
print("There are $count lines in $filename\n");
函数脚本举例
#!/usr/bin/perl
use 5.24.3;
use warnings;
use IO::File;
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.
sub main
{
my $filename = shift || "linesfile.txt"; #shift: 从数组的开头取出元素, shift(@a)删除数组第一个元素,返回删除的元素。缺省对@ARGV数组.如果在数组中不再存在元素,它返回 undef。In Perl, function arguments are passed using a special default array variable. This is using the shift function to grab the argument from the default array.
my $count = countlines( $filename );
print "There are $count lines in $filename";
}
sub countlines
{
my $filename = shift; #using shift because you notice up I passed it as a parameter to the function, and a different kind of a conditional you‘ll notice.
error("countlines: missing filename") unless $filename; # Unless the filename is defined, I‘m going to go ahead and print this error message.
# 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;
}
sub error
{
my $e = shift || ‘unkown error‘;
print "$0: $e"; # $0, that is a built in variable, a default variable, that gives the path name of our script.
exit 0;
}
复制binary files,比如windows下复制图片文件脚本举例
#!/usr/bin/perl
use warnings;
use IO::File;
my $fn1 = ‘train-station.jpg‘;
my $fn2 = ‘copy.jpg‘;
my $file1 = IO::File->new("< $fn1") or die "Cannot open file: $!";
my $file2 = IO::File->new("> $fn2") or die "Cannot open output file: $!";
$file1->binmode; #we put them in binary mode. They default to text mode
$file2->binmode;
my $buffer;
while (my $len = $file1->read($buffer, 102400)) {
$file2->print($buffer);
}
print "Done.";
原文:http://blog.51cto.com/2290153/2321930