perl的语言特点

 

perl的语言特点

2010-09-26 星期六 晴朗
1. It is an interpreted language.
2.  It supports procedural, functional, and object-orientation programming.
3. Built-in regular expressions make it extremely suitable for web application development because web application programming involves processing and parsing data. With Perl, this is trivial.
4. Processing strings in other languages isn’t as elegant as it is with Perl since regular expressions are part of Perl.
5. It has its own garbage collection — you don’t have to explicitly allocate and free required memory, a common source of errors in programming languages without garbage collection.
6. It possesses C/shell-like syntax.
7.  It’s a very loosely typed language. Variable types (scalars, arrays, hashes) are indicated by the type of sigil in front of the variable name. (使用sigil印记来标注:$,@,%,etc.)
8.  It supports scoping —lexical, global, dynamic 
9. It has a C API that allows for writing Perl programs that can internally call C programs as well as create Perl packages that interact at a lower level with a library such as a database driver.
10. It has an extensive library of Perl packages, CPAN (Comprehensive Archive Network), that provides reusable functionality for an endless number of features, needs, tasks, including the kitchen sink!
Perl’s variables are all objects.
Perl Data Types
The basic data types of Perl are scalars, arrays, hashes, file handles, type globs, and subroutines.
Perl需要显示的dereferenced——使用$$
Variable Scope
The point here is to explain scoping of variables in Perl. The Perl scoping mechanisms are:
  •  my: This is lexical scoping, meaning that the variable is only visible within the block of code it is declared in, including functions called within that block. For instance, in the following code, even though the variable $val assumes a value of the current value being iterated over, since it is declared as my, aka lexical, within that for loop (in a block, within brackets). It is not the same variable as the variable $val declared at the beginning of mysub(). The variable $val, is returned at the end of mysub(), which returns a reference to this lexical variable, giving it life beyond mysub(). This means the variable $val itself is no longer in scope, but rather a reference to it. It is returned, gives access to it, and it ‘‘survives.’’ Internally, Perl keeps a reference count of variables that is a count of any reference created for this variable. When the reference count reaches zero,
the variable is destroyed.
sub mysub {
    my $val= ‘x’;
    for (0 .. 1) {
        my $val= $_;
    }
     return $val;
}
  •  local: This is dynamic scoping, meaning dynamic variables are visible to functions called within a block where those variables are declared. In other words, if you declare a global (package) variable and in another block declare that same variable as local, the value it previously had is temporarily stashed, and the new value assigned. Once the block containing the variable scoped as local is exited, the previous original value (prior to the local assignment) is assumed. This gives local the effect of being able to temporarily override a global variable with a different value without losing the original value, hence the name dynamic scoping.
  • our: This is package scoping, meaning all subroutines have access to this variable. In previous versions of Perl, this was done by the following:
    use vars qw(var1 var2);
    $var1= ‘some value’;
    $var2= 33;
It is now done with:
    our ($var1, $var2);
Scope Example
Working code is always a good way to see a concept in action. The following example shows how scoping works:
our $foo= "our foo";
sub main {
print "main: $foo/n";
my_foo(’main’);
local_foo(’main’);
}
sub my_foo {
my ($caller)= @_;
my $foo= "my foo";
print "my_foo foo: $foo, caller $caller/n";
inner_foo(’my_foo’);
}
sub local_foo {
my ($caller)= @_;
local $foo= "local foo";
print "local_foo foo: $foo, caller $caller/n";
inner_foo(’local_foo’);
}
sub inner_foo {
my ($caller)= @_;
print "1: inner_foo foo $foo, caller $caller/n";
my $foo= "my foo";
print "2: inner_foo foo $foo, caller $caller/n";
}
main();
Notice the following about the previous example:
1. The global/package variable $foo is declared at the top level of the code, outside any subroutines.
This makes this variable visible in all subroutines.
2. The main() subroutine just prints out $foo without making any changes.
3. my_foo() declares a lexical $foo, which is not the global $foo, that will have its own
value that is only scoped within my_foo() and not available to inner_foo(), which it
then calls.
4. local_foo() scopes $foo as local, which will temporarily set the global $foo to "local foo".
This should be visible to inner_foo(), which it calls, until the end of local_foo().
5. inner_foo() first prints out whatever the current value of $foo is, then declares its own lexical
$foo, just to drive home the idea of lexical variables. Regardless of whatever value that $foo
was, whether scoped via our or local, the lexical variable will be "inner foo" until the end of
inner_foo().
The program’s output confirms the expected functionality:
main: our foo
my_foo foo: my foo, caller main
1: inner_foo foo our foo, caller my_foo
2: inner_foo foo my foo, caller my_foo
local_foo foo: local foo, caller main
1: inner_foo foo local foo, caller local_foo
2: inner_foo foo my foo, caller local_foo
local的含义与python中的global含义想法,但是作用相同,这是因为在这些动态脚本语言中没有显示的变量声明导致的。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值