Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you say <code>__PACKAGE__-&gt;run(@ARGV)</code> then <code>run</code> can be defined in a class you inherit from or allows a class to inherit from this one. If you just say <code>run(@ARGV)</code> you are missing the class information. This only matters if you are doing OO style programming.</p> <p>Put the following in a file named <code>Foo.pm</code>, and then say <code>perl Foo.pm 1 2 3</code></p> <pre><code>package Foo; use strict; use warnings; __PACKAGE__-&gt;main(@ARGV) unless caller; sub main { my $class = shift; my $obj = $class-&gt;new(@ARGV); print $obj-&gt;to_string, "\n"; } sub new { my $class = shift; return bless [@_], $class; } sub to_string { my $self = shift; return "args: " . join ", ", map { "[$_]" } @$self; } 1; </code></pre> <p>Now put the following in <code>Bar.pm</code>, and say <code>perl Bar.pm a b c</code>.</p> <pre><code>package Bar; use strict; use warnings; use base 'Foo'; __PACKAGE__-&gt;main(@ARGV) unless caller; sub to_string { my $self = shift; return "args: " . join ", ", map { "&lt;$_&gt;" } @$self; } 1; </code></pre> <p>Now lets see what happens if you don't use <code>__PACKAGE__</code> in this environment. Make the first section of code in <code>Foo.pm</code> look like this:</p> <pre><code>main(@ARGV) unless caller; sub main { my $obj = Foo-&gt;new(@ARGV); print $obj-&gt;to_string, "\n"; } </code></pre> <p>Now run <code>perl Foo.pm 1 2 3</code>. Everything should still look right. Now try running <code>perl Bar.pm a b c</code>. We still get output, but it is not the output we expect. What happens if we remove <code>__PACKAGE__</code> from <code>Bar.pm</code>? Well, we get and error: <code>Undefined subroutine &amp;Bar::main called at Bar.pm line 8.</code> This is because there is no <code>main</code> function in the module and we are not calling it with the class, so it won't look for it in the <code>Foo</code> package anymore.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload