博客
关于我
perl 总结
阅读量:280 次
发布时间:2019-03-01

本文共 1805 字,大约阅读时间需要 6 分钟。

Perl语言入门教程

基本语法

括号

在 Perl 中,括号用于赋值和初始化数组或哈希。小括号 () 用于赋值,中括号 [] 用于数组引用,大括号 {} 用于哈希引用。

  • 数组赋值

    $hash = %hash=("foo", 35, "bar", "12.4");

    这个语句初始化了一个哈希,键值对分别是 foobar 对应的值 35"12.4"

  • 列表赋值

    ($fred, $barney) = qw(a b c d);

    这个语句将数组 a b c d 分配给变量 $fred$barney

  • 空数组或空哈希

    @line = (); # 空数组$hash = %hash = (); # 空哈希

中括号与大括号

  • 中括号 用于数组下标和匿名数组的引用:

    $array_r = [1, 2, 3, 4, 5];
  • 大括号 用于匿名哈希的引用:

    $hash_r = { apple => "pomme", pear => "poire" };

    访问哈希元素:

    $hash_r{"fr"} = "flint";
  • 数组引用

    $array = (1, 2, 3, 4, 5);$array_r = $array;$array2 = @{$array_r}; # 拷贝数组

数组

列表与数组的区别

在 Perl 中,列表数组 是等价的概念,主要区别在于赋值方式:

$a = <==>  # 读取一行,标量@a = <==>  # 读取多行,数组

文件处理

使用 perl -n 模式读取文件:

perl -n 'print scalar <==>;'

加上 -n 会自动处理每一行。

while 循环

while (<==>) {  push(@a, $_);}

循环读取并存储每一行。

范围指定

perl -pe '@A=<==>; print @A[-10..-1]'

使用负值范围可以从末尾开始提取元素。

字符串处理

字符串与数组转换

  • 长度处理:

    $str = "1234567";@a = split(//, $str);

    没有分隔符时会默认分割空白。

  • 字符串长度:

    $d1 = length(@d);$d2 = 1;
  • 字符串转数组:

    $f = "@d"; # 转换为字符串$g = join("\n", @d); # 转换为多行字符串

字符串操作

  • 倒序行:
    perl -e 'print reverse <==>;'
  • 倒序字符:
    perl -ple '$_ = reverse'
  • 插入空行:
    perl -pe '$_ .= "\n\n"'
  • 插入空格:
    perl -pe 'print " "x5'

正则表达式

替换操作

  • 替换所有 <>
    $string =~ s/<.*?>/g;
  • 替换 scarletrubyred
    perl -pe 's/scarlet|ruby|puce/red/g'

区间匹配

  • 匹配两个正则表达式之间的内容:
    perl -ne 'print if /Iowa/../Montana/'
  • 删除空行和注释:
    perl -ne 'print $_ unless (/^$/ || /^\#/)'

哈希与引用

哈希操作

  • 初始化:
    %hash = ("abc" => 123, "def" => 456);
  • 访问:
    $value = $hash{"key"};
  • 引用:
    $ref = \%hash;print "$$ref{abc}"; # 输出哈希中的值

函数与闭包

  • 定义函数:
    sub function { ... }
  • 使用引用:
    @list = function($i);$LoL[$i] = \$list; # 数组引用$LoL[$i] = scalar @list; # 数组值

文件处理

读取文件

open(IN, "
;chomp(@raw);
  • 使用正则处理文件内容:
    $pattern = Pattern.compile("\\s+(.*),");$text =~ $pattern;

高效处理

Perl 在处理大文本时效率远高于 Java,例如正则匹配和字符串操作。

总结

通过以上内容,初步掌握了 Perl 的基本语法和常用操作,包括数组、字符串、正则表达式和哈希的使用。随着更多实践,熟悉这些工具会显著提升你的编程效率。

转载地址:http://hwfx.baihongyu.com/

你可能感兴趣的文章
nova基于ubs机制扩展scheduler-filter
查看>>
Now trying to drop the old temporary tablespace, the session hangs.
查看>>
nowcoder—Beauty of Trees
查看>>
np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
查看>>
np.power的使用
查看>>
NPM 2FA双重认证的设置方法
查看>>
npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
查看>>
npm build报错Cannot find module ‘webpack‘解决方法
查看>>
npm ERR! ERESOLVE could not resolve报错
查看>>
npm ERR! fatal: unable to connect to github.com:
查看>>
npm ERR! Unexpected end of JSON input while parsing near '...on":"0.10.3","direc to'
查看>>
npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
查看>>
npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
查看>>
npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
查看>>
npm install CERT_HAS_EXPIRED解决方法
查看>>
npm install digital envelope routines::unsupported解决方法
查看>>
npm install 卡着不动的解决方法
查看>>
npm install 报错 EEXIST File exists 的解决方法
查看>>
npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
查看>>
npm install 报错 Failed to connect to github.com port 443 的解决方法
查看>>