in reply to Re^6: Access a global variable from a subroutine when used as "for" iterator
in thread Access a global variable from a subroutine when used as "for" iterator

Your perception of my as global is also wrong !
Only special variables like $_, $a, $" are really global (they always belong to the main:: namespace).

It is clear that I don't know how to call a variable that could be accessed from everywhere within the script's (main?) scope. I'm not sure if "public" is a better name. There is no intention to share it with modules in order to use "our", at least at this stage of the development.

(maybe I should start writing a tutorial instead of preaching again and again ;)

That could be very useful for people like me, if that tutorial is well indexed by search engines (or the Super Search). :-D

  • Comment on Re^7: Access a global variable from a subroutine when used as "for" iterator
  • Download Code

Replies are listed 'Best First'.
Re^8: Access a global variable from a subroutine when used as "for" iterator (updated)
by LanX (Saint) on Dec 22, 2023 at 02:24 UTC
    Perl has two different kinds of variables, private variables declared with my and package variables (usually) declared with our

    • a lexical scope in Perl is anything from declaration till end of enclosing { block }
    • the biggest possible lexical scope outside a block is the file, aka file-scope
    • lexical ( i.e. "as you read" the static code ) is opposed to "dynamic" (i.e. run-time effects)
    • main:: and other package s are namespaces not scopes, think of them as hashes of variables and functions
    • my variables do not belong to namespaces, our vars do
    • global means accessible everywhere, all namespaces including vars and subs are global as long as you use the *fully::qualified::name
    • special variables always belong to main::

    for sources, e.g. "Scoping" see

    Update

    And an excellent overview can be found in

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

      Well that's confusing. Aren't these self-contradictory?

      • main:: and other package s are namespaces not scopes ...
      • variables do not belong to namespaces
      • special variables always belong to main::

      I can't reconcile these 3 statements, sorry.

      Post-update: reads much better now, thanks.


      🦛

        Hi

        Thanks corrected, somehow the "my" got lost

        > > my variables do not belong to namespaces

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        see Wikisyntax for the Monastery