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

相關文章
相關標籤/搜索