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

  • 相关阅读:
    Mockito一个方法的实例
    LIst与ArrayList区别
    mockito入门学习
    eclipse中调整字体大小和改变背景颜色
    常用sql语句
    eclipse导入代码和重新编译
    windows下登录linux的常用工具SecureCRT和常用命令
    junit4
    接口测试
    java环境
  • 原文地址:https://www.cnblogs.com/hanleilei/p/2437212.html
Copyright © 2011-2022 走看看