zoukankan      html  css  js  c++  java
  • Perl 学习手札之七: special variables

    perl uses special variable for a number of purpose.

    default value for function and loops

    error codes and messages

    information about the system and its enviroment

    over 70 special variables

      full list in perl documentation page "perlvar"

    $_           Default input

    $1, $2, $3, etc      Pattern results

    $!           system error number or string

    $@           eval() error

    $$           process ID(PID)

    $0           programe name

    @_           list of arguments for subroutine

    @ARGV        list of command line arguments

    @INC         List of path perl searches for libraries and modules

    %ENV         Hash of environment variables

    example.pl

    #!/usr/bin/perl

    use strict;
    use warnings;

    main(@ARGV);

    sub main
    {
    # message(join(":",@ARGV));
    message(join(":",@_));
    }

    sub message
    {
    my $m = shift or return;
    print("$m\n");
    }

    sub error
    {
    my $e = shift || 'unkown error';
    print("$0: $e\n");
    exit 0;
    }

    this code will demo @ARGV

    run this code in commandline, perl example.pl one two three four five

    and the result: one: two: three: four: five

    in this code, substitude the  @ARGV with @_ , we will get the same result.

    example1.pl

    #!/usr/bin/perl
    use strict;
    use warnings;

    main("five","six","seven");

    sub main
    {
    # message(join(":",@ARGV));
    message(join(":",@_));
    }

    sub message
    {
    my $m = shift or return;
    print("$m\n");
    }

    sub error
    {
    my $e = shift || 'unkown error';
    print("$0: $e\n");
    exit 0;
    }

    in this code, the default array is main("five","six","seven");

    example2.pl

    #!/usr/bin/perl

    use strict;
    use warnings;

    main("five","six","seven");
    #main(@ARGV);

    sub main
    {
    foreach (@_){
    #foreach $_ (@_){
    print;
    }
    }

    sub message
    {
    my $m = shift or return;
    print("$m\n");
    }

    sub error
    {
    my $e = shift || 'unkown error';
    print("$0: $e\n");
    exit 0;
    }

    notice: the foreach loop use default $_, 不用这个$_结果一样。

    filehandle.pl

    #!/usr/bin/perl

    use strict;
    use warnings;

    main(@ARGV);

    sub main
    {
    message("This is the template.pl exercise file from Perl 5 Essential Training.");
    error("This is the template.pl exercise file from Perl 5 Essential Training.");
    }

    sub message
    {
    my $m = shift or return;
    #print(STDOUT "$m\n");
    print(STDOUT "$m\n");# if we want to redirect the output">", STDERR will not get it
    }

    sub error
    {
    my $e = shift || 'unkown error';
    print(STDERR "$0: $e\n");
    exit 0;
    }

    notice: the default output is STDOUT.

    if we replace the STDOUT with STDERR, 我们将得到一个报错的结果: 文件名+报错信息。

    filehandle1.pl

    #!/usr/bin/perl

    use strict;
    use warnings;

    main(@ARGV);

    sub main
    {
    while(<>){
    print "$. $_ ";
    }
    }

    notice: $. represent line No

    或者我们可以用直接一个print 语句,则默认输出$_。

    constant.pl

    #!/usr/bin/perl

    use strict;
    use warnings;

    main(@ARGV);

    sub main
    {
    message("the filename is ". __FILE__);
    message("the line number is : ". __LINE__);
    message("the package is ". __PACKAGE__);
    }

    #__END__

    sub message
    {
    my $m = shift or return;
    print("$m\n");
    }

    sub error
    {
    my $e = shift || 'unkown error';
    print("$0: $e\n");
    exit 0;
    }
    __END__

    notice: __END__ is end of this script label.

  • 相关阅读:
    每天一个linux命令(57):ss命令
    [原][osg][osgearth]我眼中的osgearth
    [osg]osgDB的加载机制,使用3DS插件做参考(转,整理现有osgDB资料)
    [原][osgearth]osgearthviewer读取earth文件,代码解析(earth文件读取的一帧)
    [原][译][osgearth]Model Source Drivers模型驱动源(OE官方文档翻译)
    [原][译][osgearth]样式表style中参数总结(OE官方文档翻译)
    [原][osgearth]API加载earth文件的解析
    [原][译][osgearth]API加载地球(OE官方文档翻译)
    [osgearth]Earth文件详解
    [osgearth]通过API创建一个earth模型
  • 原文地址:https://www.cnblogs.com/hanleilei/p/2358498.html
Copyright © 2011-2022 走看看