The ABC of Perl : My First Perl Program

Hi everyone. Today I will start a new series of esssays introducing the elementary knowledge of Perl, a computer language of which probably many of us never heard. I‘ll try my best to articulate my idea. If there is something you disgree with, please feedback to me and I will seriously consider your views.

I. What‘s Perl  (Practical Extraction and Report Language)?

Just as C/C++, Perl is just another member of the kingdom of computer language. Actually it‘s a kind of old-fashioned scripting language. There was a time when most web applications were writen by Perl. Nowdays Perl is gradually taken place by Python and PHP partly because of its fecklessness, boundless flexbility and some tricky grammars. However, those defects can not obscure its virtues. Perl has a powerful weapon---Regular Expression, which make this language extraordinary when text processing. Besides, Perl is also an object-oriented programming(OOP) language. Although it‘s not as efficient as C, you can simply write several lines of perl code to implement some complicate functions, which will largely shorten development cycles.

II. What do I need to know before learing Perl?

To be honest, it‘s not a good choice if you start with perl when you know nothing about computer programing. In some ways perl is over free, which may give the learner an illusion that you can write the code in any way. Unfortunately, there are still borders. If you ignore these limits, your code will crash and you even don‘t know what‘s wrong. You‘d better know something about C or Python or any other popular languages. And if you are familiar with linux, you‘ll find it very easy to learn perl regular expressions, because RE is actually an enhanced version of awk/sed.

III. Reference Books

For a starter, the book ‘Learning Perl‘ is recommended, you can get a basic idea of perl. If you want to go further, you can read ‘Programing Perl‘ to learn some powerful tools. And if you want to know how Perl works, which means some fundamental operational mechanism, ‘Advance Perl Programing‘ will be right choice.

IV. How to Setup Perl Environment

For windows, you can download and install ActivePerl, which is a integrated script interpreter.When you finish the installation, you can open the Cmd window and type "perl -v", you‘ll see some information of perl.

For Linux users, normally ubuntu system has integrated perl interpreter. To check for that you can press Ctrl+Alt+T to call out terminal and then type perl -v.

V. Perl Editor: Sublime Text

 In fact, you can use anything to write perl codes, as long as the suffix of your file is ‘.pl‘ or ‘.pm‘. However sublime text will make the code writing more enjoyable. Now let me tell you how to use sublime text to write and run perl codes.

First search and download sublime text, then install the software.

Just do as instruction, you can easily finish the installation.

The installation is finished. Now you can write your perl codes by sublime. Note that the suffix of the file must be ".pl" or ".pm". However, you can not run your codes. To implement this function, you should move one more step.

 Open sublime text, you‘ll find option "Tools" in the toolbar. Click Tools->Build System->new Build System.

Then copy these codes into the new dialog box.

{
    "cmd": ["perl", "-w", "$file"],
    "file_regex": ".* at (.*) line ([0-9]*)",
    "selector": "source.perl"
}

Then press Ctrl+S ,save the file with the name of "Perl.sublime-build" into the default directory. Done.

Now your sublime text is able to run perl. (use Ctrl+B)

VI. First Perl Program: Hello World

Open Sublime, create a file saved as "hello.pl", type your code as follows:

#!/usr/bin/perl -w
use strict;
print "Hello World!";

1;

Then press Ctrl+B, you‘ll get the results.

‘#’  is an annotation for Perl, just as ‘/***/’  for C language.

#!/usr/bin/perl -w     is a linux-style sentence. #!/usr/bin/perl means that the system will find the perl interpreter at /usr/bin/perl, for windows users, this line is dispensable, but it‘s recommended to keep it in consideration of portability.  "-w" mean use waring mode, the interpreter will show some warning if your codes lack of standardization.

use strict;  means that the interpreter will use the most strict grammar to check your codes. It‘s better to add this line for novice.

1; replaces the return-value of this program.

print "Hello World"  means that the system print the sentence "Hello World" into the default output handle,the screen in other words.

Now you have already know how to write your first perl program. Just try more for yourself!

 

时间: 2024-11-07 20:36:05

The ABC of Perl : My First Perl Program的相关文章

[Perl系列—] 2. Perl 中的引用用法

Perl 中的引用,为什么要使用引用? 对于熟悉C语言的开发者来说, 指针这个概念一定不陌生. Perl 的引用就是指针,可以指向变量.数组.哈希表甚至子程序. Perl5中的两种Perl引用类型为硬Perl引用和符号Perl引用.符号Perl引用含有变量的名字,它对运行时创建变量名并定位很有用,基本上,符号Perl引用就象文件名或UNIX系统中的软链接.而硬Perl引用则象文件系统中的硬链接. Perl4只允许符号Perl引用,给使用造成一些困难.例如,只允许通过名字对包的符号名哈希表(名为_

Perl入门(六) Perl方法的使用

 1.定义一个方法 Perl使用sub定义方法. 语法: sub 方法名称{方法体} 2.调用一个方法 Perl直接使用方法名称调用方法. 调用方式有以下四种: 方法名称: &方法名称: 方法名称(); &方法名称(); 说明:方法调用可以再任何位置,可以在方法前.后调用,也可以在方法体内部调用. 3.传递参数 Perl通过方法名后面的括号将参数列表传递到方法体内.例如:function_name("param1","param2"...); 方

perl 语法速查

同时学perl.python和shell脚本会很容易将它们的语法搞混,本文主要是一个个人的总结,方便要用时的查询. perl基本语法.安装.帮助文档 文件头格式: #!/usr/bin/perl use strict; use warnings; 运行perl脚本: #调用perl程序 perl test.pl #可执行脚本 chmod 755 test.pl ./test.pl 基本读写: #键盘输入 $a = <STDIN>; chomp($a); print $a; #文件输入 open

perl学习笔记之:模式匹配,模块,文档

Perl语言的最大特点,也是Perl作为CGI首选语言的最大特点,是它的模式匹配操作符.Perl语言的强大的文本处理能力正是通过其内嵌的对模式匹配的支持体现的.模式通过创建正则表达式实现.Perl的正则表达式与模式匹配的特点一是内嵌于语言之中,而不是通过库或函数来实现,因此使用更简便:二是比一般的正则表达式与模式匹配功能强大. 模式匹配操作符简介 操作符 意义 实例 =~ 匹配(包含) !~ 不匹配(不包含) m// 匹配 $haystack =~ m/needle/ $haystack =~

perl学习笔记之:正则表达式

 Perl 中的正则表达式 正则表达式的三种形式  正则表达式中的常用模式  正则表达式的 8 大原则          正则表达式是 Perl 语言的一大特色,也是 Perl 程序中的一点难点,不过如果大家能够很好的掌握他,就可以轻易地用正则表达式来完成字符串处理的任务,当然在 CGI 程序设计中就更能得心应手了.下面我们列出一些正则表达式书写时的一些基本语法规则. ----------------------------------------------------------------

perl学习之正则表达式

9    Perl 中的正则表达式 正则表达式的三种形式 正则表达式中的常用模式 正则表达式的 8 大原则 正则表达式是 Perl 语言的一大特色,也是 Perl 程序中的一点难点,不过如果大家能够很好的掌握他,就可以轻易地用正则表达式来完成字符串处理的任务,当然在 CGI 程序设计中就更能得心应手了.下面我们列出一些正则表达式书写时的一些基本语法规则. -----------------------------------------------------------------------

在vi中使用perltidy格式化perl代码

格式优美的perl代码不但让人赏心悦目,并且能够方便阅读. perltidy的是sourceforge的一个小项目,在我们写完乱七八糟的代码后,他能像变魔术一样把代码整理得漂美丽亮,快来体验一下吧!!! perltidy 主页: http://perltidy.sourceforge.net/perltidy.html 安装方法: 进入解压后的文件夹,然后运行一下命令 perl Makefile.PL make make test make install 用法: 配置一下vim,使得我们在写代

Perl 中的正则表达式

原帖地址:http://263.aka.org.cn/Lectures/002/Lecture-2.1.2/perl-reg.html [正则表达式的三种形式] 首先我们应该知道 Perl 程序中,正则表达式有三种存在形式,他们分别是: 匹配:m/<regexp>/ (或 /<regexp>/ ,略去 m) 替换:s/<pattern>/<replacement>/ 转化:tr/<pattern>/<replacemnt>/ 这三种形

perl基本语法

标量 标量是 Perl 中最简单的数据类型.大多数的标量是数字(如 255 或 3.25e20)或者字符串(如 hello或者盖茨堡地址). 数字 perl中所有数字内部的格式都是双精度浮点数. 浮点数 1.25 255.000 255.0 7.25e45 #7.25x10 的 45 次方(一个大整数) -6.5e24 # -6.5x10 的 24 次方(一个大的负数) -12e-24 #- -12x10 的-24 次方(很小的负数) -1.2E-23 #指数符号可以大写(E) 整数 0 200