in reply to Re: difference between my and local
in thread difference between my and local

Good examples, I've never used stict and warnings, but now those make more sense too.

Can someone give me an example in which the difference between our and my gets clear ?
Thanks a lot!!!!!!
Luca

ps I should have added our too to the title of this post

Replies are listed 'Best First'.
Re^3: difference between my and local
by blazar (Canon) on Nov 15, 2005 at 13:57 UTC
    $ perl -Mstrict -lne 'our $c += () = /foo/g; END { print $c }' abc foo foo foo 3 $ perl -Mstrict -lne 'my $c += () = /foo/g; END { print $c }' abc foo foo foo 1

    Ok: always use strict. But not in one-liners...

    So our is somewhat "exotic" in that it declares a package variable, but in a lexical scope.

    Update: removed the statement modifiers that had inadvertently (yes!) slipped in as per Perl Mouse's remark

      If that's the best one can do to explain the difference between our and my, I'd have to say the Anon troll who's been going round lately proclaiming Perl is dead is right.

      Djees. my in combination with statement modifiers are awkward, and in some cases broken. Could you explain why the latter code prints 1 and not 3 (or throw an error?). And why the former prints 3, and not the same result as the latter? (Hint, it has to do with the runtime effects of my).

      So our is somewhat "exotic" in that it declares a package variable, but in a lexical scope.
      Eh, not quite. It declares a package variable - but because it's a package variable, it's not lexically scoped. All that's lexically scoped is the name.
      { # Scope 1 our $s = "foo"; # Sets $main::s } { # Different scope. print our $s; # *Same* variable! } __END__ foo
      our just means: from now on, till the end of the innermost enclosing block, whenever I refer to the variable(s) following our, I mean the package variables with the same name, in the current package (as per use of our, not necessarely per use of the variable).

      So, my gives you a new lexical variable, local gives you a temporary (not lexical!) value, and our gives you a lexical alias or shortcut.

      Perl --((8:>*
        If that's the best one can do to explain the difference between our and my, I'd have to say the Anon troll who's been going round lately proclaiming Perl is dead is right.

        Well, it was meant partly as a joke. But laugh as much as you like, this is how I really understood our "back in the days..." so maybe it may benefit the OP as well.

        Just for learning purposes I wondered: "how would I do (something like) the following

        $ perl -lne '$c += () = /foo/g; END { print $c }'
        if I (were mad enough to) put put it under strictures?"

        Ok, I thought, if I were not a one-liner using -ln, then I'd do

        my $c; while (<>) { $c += () = /foo/g; } print $c, "\n";
        but a lexical variable wouldn't be appropriate for the previous situation, as it would be localized to the block realizing the body of the implicit while loop.

        The obvious answer would be to fully qualify $c as $main::c. The remaining question was "anything more concise?" and the answer to this question would have been in the old days use vars; but I never really liked it because I had to pass the variables I wanted as strings and I knew it did symbol table manipulations, always conveying the impression of a dirty hack over which true syntactic sugar would have been preferrable. Eventually I realized that our was the syntactic sugar and the good shortcut I was looking for.

        Hopefully what I learnt resulted to be useful also in other contexts and not only in knowing how to write strict-safe one-liners... ;-)