zoukankan      html  css  js  c++  java
  • Perl 学习手札之十六: New features in Perl 5.10

    Using Perl 5.10 features

    perl 5.10 includes new language features

     incompatible with previous version

    use feature ':5.10';

    use feature qw(switch say state);

    use 5.010

    say feature.

    #!/usr/bin/perl
    #

    use strict;
    use warnings;
    use feature ":5.10";

    main(@ARGV);

    sub main
    {
        say "This is the Perl 5.10 new features exercise file.";
        say "this is another line";
    }

    sub error
    {
        my $e = shift || 'unkown error';
        my $me = ( split(/[\\\/]/, $0 ) )[-1];
        print("$me: $e\n");
        exit 0;
    }

    switch feature:

    switch.pl

    #!/usr/bin/perl
    #

    use strict;
    use warnings;
    use feature ":5.10";

    main(@ARGV);

    sub main
    {
        my $s='jimi hendrix';#5
        given($s){
            when(undef){say'$s is undefined'}
            when('jimi'){say'$s is musician'}
            when(/jimi/){say'$s maybe a muscian'}
            when([1,3,5,7,9]){say'$s is odd number'}
            default{say '$s is something else!'}
        }
    }

    sub error
    {
        my $e = shift || 'unkown error';
        my $me = ( split(/[\\\/]/, $0 ) )[-1];
        print("$me: $e\n");
        exit 0;
    }

    state feature;

    state.pl

    #!/usr/bin/perl
    #

    use strict;
    use warnings;
    use feature ":5.10";

    main(@ARGV);

    sub main
    {
        my $i = 5;
        increment($i);
        increment($i);
        increment($i);
        increment($i);
    }

    sub increment{
        state $n = shift;
        say ++$n;
    }

    sub error
    {
        my $e = shift || 'unkown error';
        my $me = ( split(/[\\\/]/, $0 ) )[-1];
        print("$me: $e\n");
        exit 0;
    }

    注意此时的state关键字,如果用my替换,运行结果会不同!

    到此为止,Perl学习手札的更新,告一段落。进阶更新待定

  • 相关阅读:
    在vscode中显示空格和tab符号
    如何正确理解关键字"with"与上下文管理器
    HADOOP基本操作命令
    Ganglia环境搭建并监控Hadoop分布式集群
    关于分布式系统的数据一致性问题
    hadoop snapshot 备份恢复 .
    hadoop主节点(NameNode)备份策略以及恢复方法
    HDFS snapshot操作实战
    从 secondarynamenode 中恢复 namenode
    hadoop 通过distcp进行并行复制
  • 原文地址:https://www.cnblogs.com/hanleilei/p/2437212.html
Copyright © 2011-2022 走看看