Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.

  • $ perl -wle 'package foo; our $_; print map {$_} 1..5'
    Use of uninitialized value in print at -e line 1.
    Use of uninitialized value in print at -e line 1.
    Use of uninitialized value in print at -e line 1.
    Use of uninitialized value in print at -e line 1.
    Use of uninitialized value in print at -e line 1.

    What happened? Well $_ is supposed to be $main::_. But after the our, $_ is now looking at $foo::_ while map still populates $main::_.

    You may find http://www.perlmonks.org/?node_id=48379 [perlmonks.org] to be of interest as wel
  • So it would be a flaw in a Perl file, if it contains multiple packages, with our statements in them... or at least a critic-worthy warning?
    • File-scoped our variables could be a problem. Block-scoped our variables are less troublesome.

  • Another small problem with our variables is that they take twice the memory of lexical variables or plain global.

    --
    Close the world. txEn eht nepO
    • That comment is a bit misleading; if you have a 1000-byte string in an our variable it's not suddenly going to consume 2000 bytes. It would be more accurate to say they take twice the overhead.
  • So in short: "our" creates a lexically scoped alias for a package global variable. And lexical scope itself has nothing to do with packages, but with curlies and files.