When setting up Perl code to run as either a script or module, what is the reason for __PACKAGE__?

前端 未结 1 1995
星月不相逢
星月不相逢 2021-01-06 09:32

In this earlier Stackoverflow question and especially brian d foy\'s \"How a Script Becomes a Module\" I\'ve read about how to set up code so that it can be run as either a

1条回答
  •  一整个雨季
    2021-01-06 10:12

    If you say __PACKAGE__->run(@ARGV) then run can be defined in a class you inherit from or allows a class to inherit from this one. If you just say run(@ARGV) you are missing the class information. This only matters if you are doing OO style programming.

    Put the following in a file named Foo.pm, and then say perl Foo.pm 1 2 3

    package Foo;
    
    use strict;
    use warnings;
    
    __PACKAGE__->main(@ARGV) unless caller;
    
    sub main {
        my $class = shift;
        my $obj   = $class->new(@ARGV);
        print $obj->to_string, "\n";
    }
    
    sub new {
        my $class = shift;
        return bless [@_], $class;
    }
    
    sub to_string {
        my $self = shift;
        return "args: " . join ", ", map { "[$_]" } @$self;
    }
    
    1;
    

    Now put the following in Bar.pm, and say perl Bar.pm a b c.

    package Bar;
    
    use strict;
    use warnings;
    
    use base 'Foo';
    
    __PACKAGE__->main(@ARGV) unless caller;
    
    sub to_string {
        my $self = shift;
        return "args: " . join ", ", map { "<$_>" } @$self;
    }
    
    1;
    

    Now lets see what happens if you don't use __PACKAGE__ in this environment. Make the first section of code in Foo.pm look like this:

    main(@ARGV) unless caller;
    
    sub main {
        my $obj = Foo->new(@ARGV);
        print $obj->to_string, "\n";
    }
    

    Now run perl Foo.pm 1 2 3. Everything should still look right. Now try running perl Bar.pm a b c. We still get output, but it is not the output we expect. What happens if we remove __PACKAGE__ from Bar.pm? Well, we get and error: Undefined subroutine &Bar::main called at Bar.pm line 8. This is because there is no main function in the module and we are not calling it with the class, so it won't look for it in the Foo package anymore.

    0 讨论(0)
提交回复
热议问题