Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: the "our" declaration ?!!

by ikegami (Patriarch)
on Jan 20, 2009 at 04:56 UTC ( [id://737479]=note: print w/replies, xml ) Need Help??


in reply to the "our" declaration ?!!

It's kinda like no strict 'vars';, but it only applies to a single variable.

It means "yes, a package variable with that name exists, so don't complain about it."

Replies are listed 'Best First'.
Re^2: the "our" declaration ?!!
by codeacrobat (Chaplain) on Jan 20, 2009 at 07:18 UTC
    The strict pragma only protects the current namespace as you can see in some oneliner examples below.
    # set variable, check items in PKG symbol table $ perl -Mstrict -e '$PKG::foo=1; print keys %PKG::' foo $ perl -Mstrict -e '$foo=1; print keys %main::' Global symbol "$foo" requires explicit package name at -e line 1. Execution of -e aborted due to compilation errors. $ perl -Mstrict -e 'our $foo=1; print grep {/foo/} keys %main::' foo

    print+qq(\L@{[ref\&@]}@{['@'x7^'!#2/"!4']});

      I don't understand what you are saying, and your code doesn't contradicts anything I said.

      >perl -Mstrict -le"{ package PKG; $foo=1; }" Global symbol "$foo" requires explicit package name at -e line 1. Execution of -e aborted due to compilation errors. >perl -Mstrict -le"{ package PKG; no strict 'vars'; $foo=1; } print fo +r grep /foo/, keys %main::" >perl -Mstrict -le"{ package PKG; our $foo=1; } print for grep /foo/, +keys %main::" >perl -Mstrict -le"{ package PKG; no strict 'vars'; $foo=1; } print fo +r grep /foo/, keys %PKG::" foo >perl -Mstrict -le"{ package PKG; our $foo=1; } print for grep /foo/, +keys %PKG::" foo
      The strict pragma only protects the current namespace

      No, remove the use strict; and the below will run without complaint.

      package X; use strict; sub one { 1 } package Y; $yy = 1; package main; exit; __DATA__ Global symbol "$yy" requires explicit package name at x line 6. Execution of x aborted due to compilation errors.
      Be well,
      rir

      Update: added initial quote for clarity

Re^2: the "our" declaration ?!!
by LanX (Saint) on Jan 20, 2009 at 18:00 UTC
    It's kinda like no strict 'vars';, but it only applies to a single variable.

    It means "yes, a package variable with that name exists, so don't complain about it."

    Thats a common but incomplete interpretation. "our" works also useful without "strict", so it's not about complaining or not.

    And there are important differences to no strict 'vars', because the chosen namespace can be very different from what you expect: With our it's the stash of the package at declaration position, like with my where it's the lexpad at declaration position.

    But with no strict 'vars' a "non-explicit" variable belongs to the actual package at the encounter position!

    Cheers Rolf

    UPDATE: So one can't simply refactor a code be replacing no strict 'vars' with a lot of ours for each variablename. You have to take care about the local packages!

      Thats a common but incomplete interpretation.

      Correct, but only if you allow for poorly scoped variables. As I see things, my explanation is complete except in buggy programs. Put curlies around your packages when you have more than one in your file.

        don't know what you mean, the following code is with curlies but produces different output for the two options.
        use strict; no strict "vars"; # option1 #our $x; # option2 $x="main"; { package other; $x="other"; print $x; } print $x; __END__ 1: othermain 2: otherother

        Cheers Rolf

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://737479]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-23 23:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found