`
arganzheng
  • 浏览: 104049 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

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含义想法,但是作用相同,这是因为在这些动态脚本语言中没有显示的变量声明导致的。

分享到:
评论

相关推荐

    Perl语言学习.pdf

    Perl语言是由Larry Wall在20世纪80年代开发的一种开源的高级语言,它结合了低级语言和高级语言的特性,具有速度快、灵活性强和可读性好的特点。Perl语言的产生是为了解决awk语言无法生成报表的问题,Larry Wall想要...

    Perl语言编程.pdf

    Perl语言的主要特点包括: * 高级语言:Perl语言是一种高级语言,它支持面向对象编程、函数编程和过程编程等多种编程范式。 * 通用语言:Perl语言可以应用于多种领域,包括文本处理、网络编程、数据库交互、系统...

    perl语言入门+perl语言编程

    Perl语言的一大特点是其“ TIMTOWTDI”哲学,即“There Is More Than One Way To Do It”,这鼓励程序员以多种方式实现同一目标,从而增加了代码的多样性和创造性。然而,这也可能导致代码风格不一致,因此在实际...

    perl语言详解--全方位解读perl语言

    #### 二、Perl语言的特点与优势 1. **简单性与灵活性**:Perl语言的核心设计理念之一便是“简单”。它能够使常见的编程任务变得更加容易,如文本处理、文件操作等,同时又具备处理复杂问题的能力。这一特点使得Perl...

    Perl语言在生物信息学中的应用学习教案.pptx

    Perl语言在生物信息学中的应用学习教案 Perl 语言是实用摘录和报告语言(Practical Extraction and Report Language),但它其实不是缩写。 Larry Wall 创建了 Perl 语言,它有着悠久的历史。 Perl 语言在生物信息...

    perl语言自学手册

    通过阅读"Perl语言编程.CHM"、"Perl语言入门.pdf"和"Perl5语言全教程.rar"这三份文档,你可以深入学习Perl的基本语法、高级特性和实际应用案例。它们将帮助你从零开始掌握Perl,无论是进行简单的文本处理任务,还是...

    PERL语言编程.pdf

    正则表达式是Perl语言的一大特色,被广泛用于文本搜索和替换操作,相关的知识点包括量词、最小匹配和元字符等。列表处理在Perl编程中也非常重要,它涉及如何高效地处理数据序列。 书的第二章“集腋成裘”深入讲解了...

    perl 语言的详细介绍

    Perl语言的特点之一是其灵活性和表达能力强大。它拥有丰富的内建数据类型,如标量、数组、哈希等,支持多种操作符,使得代码可以简洁而高效地表达复杂的逻辑。此外,Perl的正则表达式功能尤为强大,对于文本处理和...

    perl语言入门精粹

    Perl 语言特点** Perl 语言的特点包括灵活性、可移植性、强大的文本处理能力以及丰富的内置函数。Perl 的语法借鉴了多种语言,如C、sed、awk等,因此对于熟悉这些语言的开发者来说,学习 Perl 相对容易。此外,Perl...

    Perl语言在电路设计中的应用

    首先,标题中提到的Perl语言,是一种高级编程语言,它的特点在于处理文本的能力较强,且具有良好的跨平台特性,特别是在Unix系统中运行效率高。在早期的信息技术应用中,Perl的脚本编程被广泛用于系统管理、网络监控...

    perl语言编程经典教程

    ### Perl语言编程经典教程知识点概览 #### 一、Perl简介与特点 - **定义**:Perl是一种高级脚本语言,被广泛应用于系统管理、Web开发、网络编程、文本处理等多个领域。 - **特点**: - 高度灵活性:支持面向过程、...

    Perl语言编程(清晰完整)

    Perl语言的特点在于其语法灵活,允许程序员以多种风格编写代码,这被称为"Perl哲学"——"There's more than one way to do it"(TIMTOWTDI)。这种灵活性使得Perl能够适应各种编程任务,但同时也可能造成代码可读性...

    Perl语言入门(第四版)及习题答案

    Perl语言是一种功能强大的脚本编程语言,尤其在文本处理、系统管理、网络编程等领域有着广泛的应用。《Perl语言入门(第四版)》是一本专为初学者设计的教程,旨在帮助读者快速掌握Perl的基础知识和核心概念。第四版的...

    perl语言个人学习总结教程

    Perl语言的特点: 1. 简写和默认值众多,这使得编写简单而功能强大的脚本成为可能。 2. 与HTML格式兼容性良好,易于将数据转换成Web页面。 3. Perl不适用于产生二进制码,主要因为它的解释执行和运行时编译的特性。 ...

    PERL 语言入门与提高

    在Perl语言的使用中,有很多模块可供使用,这些模块都存放在CPAN(Comprehensive Perl Archive Network)上。Perl的模块几乎覆盖了所有可能的领域,比如Perl/TK、Perl/Internet、Perl/DB、Perl/Bio-info、Perl/CGI、...

Global site tag (gtag.js) - Google Analytics