Learn Perl in Y minutes

转载 2013年12月04日 18:35:54

zz: http://learnxinyminutes.com/docs/perl/


Learn X in Y minutes

Where X=perl

Perl 5 is a highly capable, feature-rich programming language with over 25 years of development.

Perl 5 runs on over 100 platforms from portables to mainframes and is suitable for both rapid prototyping and large scale development projects.


# Single line comments start with a hash.


#### Perl variable types

#  Variables begin with the $ symbol.
#  A valid variable name starts with a letter or underscore,
#  followed by any number of letters, numbers, or underscores.

### Perl has three main variable types: scalars, arrays, and hashes.

## Scalars
#  A scalar represents a single value:
my $animal = "camel";
my $answer = 42;

# Scalar values can be strings, integers or floating point numbers, and Perl will automatically convert between them as required.

## Arrays
#  An array represents a list of values:
my @animals = ("camel", "llama", "owl");
my @numbers = (23, 42, 69);
my @mixed   = ("camel", 42, 1.23);



## Hashes
#   A hash represents a set of key/value pairs:

my %fruit_color = ("apple", "red", "banana", "yellow");

#  You can use whitespace and the "=>" operator to lay them out more nicely:

my %fruit_color = (
        apple  => "red",
        banana => "yellow",
        );
# Scalars, arrays and hashes are documented more fully in perldata. (perldoc perldata).

# More complex data types can be constructed using references, which allow you to build lists and hashes within lists and hashes.

#### Conditional and looping constructs

# Perl has most of the usual conditional and looping constructs.

if ( $var ) {
    ...
} elsif ( $var eq 'bar' ) {
    ...
} else {
    ...
}

unless ( condition ) {
                   ...
               }
# This is provided as a more readable version of "if (!condition)"

# the Perlish post-condition way
print "Yow!" if $zippy;
print "We have no bananas" unless $bananas;

#  while
  while ( condition ) {
                   ...
               }


# for and foreach
for ($i = 0; $i <= $max; $i++) {
                   ...
               }

foreach (@array) {
                   print "This element is $_\n";
               }


#### Regular expressions

# Perl's regular expression support is both broad and deep, and is the subject of lengthy documentation in perlrequick, perlretut, and elsewhere.  However, in short:

# Simple matching
if (/foo/)       { ... }  # true if $_ contains "foo"
if ($a =~ /foo/) { ... }  # true if $a contains "foo"

# Simple substitution

$a =~ s/foo/bar/;         # replaces foo with bar in $a
$a =~ s/foo/bar/g;        # replaces ALL INSTANCES of foo with bar in $a


#### Files and I/O

# You can open a file for input or output using the "open()" function.

open(my $in,  "<",  "input.txt")  or die "Can't open input.txt: $!";
open(my $out, ">",  "output.txt") or die "Can't open output.txt: $!";
open(my $log, ">>", "my.log")     or die "Can't open my.log: $!";

# You can read from an open filehandle using the "<>" operator.  In scalar context it reads a single line from
# the filehandle, and in list context it reads the whole file in, assigning each line to an element of the list:

my $line  = <$in>;
my @lines = <$in>;

#### Writing subroutines

# Writing subroutines is easy:

sub logger {
    my $logmessage = shift;
    open my $logfile, ">>", "my.log" or die "Could not open my.log: $!";
    print $logfile $logmessage;
}

# Now we can use the subroutine just as any other built-in function:

logger("We have a logger subroutine!");

Using Perl modules

Perl modules provide a range of features to help you avoid reinventing the wheel, and can be downloaded from CPAN (http://www.cpan.org/). A number of popular modules are included with the Perl distribution itself.

perlfaq contains questions and answers related to many common tasks, and often provides suggestions for good CPAN modules to use.

Further Reading




Learn Python in Y Minutes(有时间再慢慢翻译)

# Single line comments start with a number symbol.""" Multiline strings can be written using thr...
  • aaa946231
  • aaa946231
  • 2016年02月24日 18:14
  • 697

learn x in y minutes

http://learnxinyminutes.com/
  • ssmale
  • ssmale
  • 2013年07月29日 11:36
  • 637

Learn clojure in Y minutes

Learn X in Y minutes Where X=clojure Get the code: learnclojure.clj Clojure is a Lisp family l...
  • hanhuili
  • hanhuili
  • 2014年03月26日 17:41
  • 1162

Learn X in Y minutes

learnxinyminutes.com,这是一个由社区驱动的语言风暴网站,含有很多语言的快速入门和示例,能够让你在短短几分钟之内了解一个语言的基本语法、设计思想、应用场景等。文档内容丰富,包含Pyt...
  • freeking101
  • freeking101
  • 2017年07月10日 15:54
  • 324

Learn Python in 10 Minutes

摘自:http://www.developertutorials.com/tutorials/python/learn-python-in-10-minutes-7-12-12/page1.htmlL...
  • youngster21
  • youngster21
  • 2010年04月05日 01:54
  • 665

Learn Perl in about 2 hours 30 minutes

转载请注明出处 http://www.oschina.net/translate/learn-perl-in-2-hours Perl语言是一门高级解释型动态语言,它的许多数据类型是运行时才确...
  • u012467492
  • u012467492
  • 2015年07月17日 14:50
  • 206

Lua 15 分钟快速入门(Learn Lua in 15 Minutes)

原文出处: tylerneylon-- 单行注释 --[[ [多行注释] --]]变量 & 控制流num = 23 -- 数字都是双精度 str = 'aspythonstring' -- 像...
  • sdfsdr
  • sdfsdr
  • 2016年04月27日 09:45
  • 424

Learn HTML5 in 5 Minutes!

There’s no question, HTML5 is a hot topic for developers. If you need a crash course to quickly unde...
  • u013067629
  • u013067629
  • 2014年10月11日 11:35
  • 1266

Learn UNIX in 10 minutes

FREEENGINEER.ORG Learn UNIX in 10 minutes. Version 1.3 PrefaceThis is something that I had given o...
  • qsdnet
  • qsdnet
  • 2007年03月04日 15:12
  • 975

Learn Lua in 15 Minutes

Learn Lua in 15 Minutes
  • helei001
  • helei001
  • 2015年11月30日 14:28
  • 617
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Learn Perl in Y minutes
举报原因:
原因补充:

(最多只允许输入30个字)