Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's covered in <a href="http://perldoc.perl.org/perlmod.html" rel="nofollow">Perlmod</a>.</p> <p>The <code>::</code> is really a <em>namespace</em> identifier. In early Perl before this idea of namespace took hold, you could have variable name collisions happening in your program:</p> <p>Here's my program:</p> <pre><code>#! /usr/bin/env perl #use strict; use warnings; use feature qw(say); require "test2.pl"; $foo = "bar"; futz_with_foo(); say $foo; </code></pre> <p>I set <code>$foo</code> to <em>bar</em> and never touched it. Should print out <code>bar</code> However, when I run my program, I get:</p> <pre><code>$ ./test.pl WOO WOO! I MESSED WITH FOO! </code></pre> <p>In <code>test2.pl</code>, I have:</p> <pre><code>sub futz_with_foo { $foo = "WOO WOO! I MESSED WITH FOO!" } 1; </code></pre> <p>What happened is that both programs are using the same variable <code>$foo</code>. To get around this issue Perl gives modules the ability to have their own namespace. The original syntax was the single quote, but changed to <code>::</code> in Perl 4 (if I remember correctly). You can still use the single quote. You declare your namespace with the <a href="http://perldoc.perl.org/fuctions/package.html" rel="nofollow">package</a>.</p> <p>The best way to understand this is to see it in action. Try the following:</p> <pre><code>#! /usr/bin/env perl use strict; use warnings; use feature qw(say); our $foo = 'This is the value of $foo'; say '$foo: ' . $foo; say '$main::foo: ' . $main::foo; say "\$main'foo: " . $main'foo;; say "\nSwitching to package Bar"; package Bar; our $foo = 'This is in package Bar'; say '$foo: ' . $foo; say '$Bar::foo: ' . $Bar::foo; say "\nSwitching to package main again"; package main; say '$foo: ' . $foo; </code></pre> <p>Running this, I get:</p> <pre><code>$foo: This is the value of $foo $main::foo: This is the value of $foo $main'foo: This is the value of $foo Switching to package Bar $foo: This is in package Bar $Bar::foo: This is in package Bar Switching to package main again $foo: This is in package Bar </code></pre> <p>By default, your program starts out in the main <em>namespace</em>. By the way, you'll notice that I declared <code>our $foo</code> and not <code>my $foo</code>. That's because <code>our</code> puts the variable in the Perl symbol table where <em>package</em> variables are stored. The <code>my</code> is a lexically scoped declaration, and is now preferred. A variable declared with <code>my</code> only exists in its declared scope and that can't be outside of the file its in.</p> <p>Maybe this will shed some light on the error message you get when you forget to declare a variable with <code>my</code>:</p> <pre><code>Global symbol "$foo" requires explicit package name at ... </code></pre> <p>By default, all Perl variables are <em>Package</em> variables (that is, they're in Perl's symbol table). The <code>use strict</code> pragma forces you to either declare package variables with <code>our</code> or forces you to use the full package name of the variable. By declaring a variable with <code>my</code> (like we do 99 40/100% of the time) and by using <code>strict</code>, we force you to declare your variables when using Perl.</p> <p>Hope this helps.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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