• 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学习手札的更新,告一段落。进阶更新待定

  • 相关阅读:
    RocketMQ延迟消息的代码实战及原理分析
    如何做技术选型?Sentinel 还是 Hystrix?
    什么是服务熔断?
    降级-熔断-限流-傻傻分不清楚
    java-分布式-降级 熔断 限流
    java-分布式-分布式事务
    常用限流算法的应用场景和实现原理
    使用Redis作为分布式锁的一些注意点
    ansible {{}}引用变量,变量中嵌套变量如何表示
    shell获得java进程号跟进程对应的线程号
  • 原文地址:https://www.cnblogs.com/hanleilei/p/2437212.html
Copyright © 2020-2023  润新知