玩转 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.web

下载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/windows

运行Komodo Edit,
右边空白处Add, New command
玩转 Perl ABC数组

Command name随便取,我这里用mine
C:\Perl64\bin\perl.exe "%F" (%F will have Perl run the script that is currently open for editing)
玩转 Perl ABCless

"%D" (%D will run the script in the current Directory)
玩转 Perl ABCide

Ctrl+Shift+R (运行Perl script)
玩转 Perl ABC函数

左边存放perl文件
玩转 Perl ABCui

一个示例
#!/usr/bin/perl
print "This is my first perl program\n";
$a=<>;
print $a;this

说明:
第一行: #!/usr/bin/perl 由什么程序执行如下的内容
注释:#
输入:<>
输出:printlua

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

open the file - with simple error reporting

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;
}

count the lines

my $count = 0;
while( $fh->getline ) {
$count++;
}

close and print

$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.

entry point

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";
}

countlines ( filename ) - count the lines in a file

returns the number of lines

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;

}

error ( string ) - display an error message and exit

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.";

相关文章
相关标签/搜索