in reply to Re^5: 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

OK, there are some ambiguities in your description, so I'm not sure I understand your intentions in total.

Your perception of my as global is also wrong !

Only special variables like $_, $a, $" are really global (they always belong to the main:: namespace).

Package variables are "public", but often protected by the need to fully qualify them, like 'pck::name'.

my's are private to the lexical scope, which is at most the file scope, which some people try to describe as "file-global".

our is a way to define a lexical shortcut to a package var.

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

In your case I'd suggest using package vars, because you can easily access the vars after exporting the subs.

The following demo is explicitly using two packages to fake an import situation. you can simplify it in a one-file-for-all situation. But it should help you grasp the differences between different var types.

use v5.14; use warnings; { package _csv; # some package name our $row; our $col; sub c { my $c = shift // $col // die "col undefined"; my $r = shift // $row // die "row undefined"; say "c:$c r:$r"; } say "--- inside same scope"; for $row (1..3) { c("A"); } } package main; BEGIN { *c = \&_csv::c } # fake import for demo say "--- one default"; for $_csv::row (1..3) { c("B"); } say "--- two defaults"; for $_csv::col ("C".."D") { for $_csv::row (4..5) { c(); } } say "--- explicit local "; { local $_csv::row = 10; c("E"); c("F"); } say "--- defaults were localized inside loop"; c("G");

perl ~/perl/cell_dsl.pl --- inside same scope c:A r:1 c:A r:2 c:A r:3 --- one default c:B r:1 c:B r:2 c:B r:3 --- two defaults c:C r:4 c:C r:5 c:D r:4 c:D r:5 --- explicit local c:E r:10 c:F r:10 --- defaults were localized inside loop row undefined at /home/lanx/perl/cell_dsl.pl line 12.

But frankly, if I were you, I'd rather define multiple subs with special explicit behavior instead of overloading one with implicit DWIM.

Or at least, use them both together and let the implicit one call the explicit ones.

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

Replies are listed 'Best First'.
Re^7: Access a global variable from a subroutine when used as "for" iterator
by vitoco (Hermit) on Dec 22, 2023 at 00:45 UTC

    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

      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.


        🦛