1

Below is a simplified version of a perl script I am trying to modify:

use MODULE_1 ;
use MODULE_2 ;
use vars qw(%ARR  $VarZZ) ;

sub A {
    # Somestuff

    # Call to Sub B
    B() ;
    # Call to Sub C
    C() ;
}

BEGIN {
    package XYZ ;
    use vars qw($VarYY  $VarXX) ;
    # MISC SUBS HERE
    # end of package XYZ
}

sub B {
    # Somestuff
}

sub C {
    # Somestuff to set %ARR
}

# Call to Sub A
A() ;

My issue is that I will like to access %ARR within the package XYZ BEGIN block but keep getting error messages saying I need to define %ARR ("requires explicit package name")

I have tried, trying to copy a similar example within the block, $main::ARR{index} but failed so far.

I assume it may be because %ARR isn't set when that block is begin evaluated and that I need to call "sub C" perhaps but &main::C(); seems to be failing as well.

How can I access this array there?

I have looked at: Perl's main package - block syntax - pragmas and BEGIN/END blocks which seems to be addressing similar themes but struggling to properly understand the answers

** EDIT **

Expanded skeleton script showing some attempts at moving forward:

use MODULE_1 ;
use MODULE_2 ;
use vars qw(%ARR  $VarZZ) ;

sub A {
    # Somestuff

    # Call to Sub B
    B() ;
    # Call to Sub C
    C() ;
    # Call to Sub E
    E() ;
}

sub E {
    # Call to Package XYZ subs
}

BEGIN {
    package XYZ ;
    use vars qw($VarYY  $VarXX %ARR) ;

    # I tried to Call to Sub C and load a local version of %ARR
    #
    # This fails with "Undefined subroutine &main::C" error
    &main::C() ;
    #
    # We never get here so not sure if correct
    %ARR = &main::ARR ;


    # MISC SUBS HERE
        sub X {
            # Call to Sub D
            &main::D() ;
        }
    # end of package XYZ
}

sub B {
    # Somestuff
}

sub C {
    # Somestuff to set %ARR
}
sub D {
    # Somestuff
}

# Call to Sub A
A() ;

Note the the call to &main::E() works when called within the Subs in Package XYZ but both this and &main::C() fail when running free standing. Perhaps the free standing call is done at complile time before the subs are defined.

BTW, I tried the our definition but getting a 502 error: Nginx Debug Log

Perhaps this is because the array is not available?

7
  • Have you also looked at my, our, local and state. I think its related to scoping mechanisms of perl since your problem seems to be rooted there. Jan 12, 2015 at 0:40
  • just using $main::ARR{...} / %main::ARR will get the right variable (as would declaring our %ARR; as @mob suggests), but only you would be able to tell if the subs in XYZ:: end up getting called before C() is called and the variable is set as you expect.
    – ysth
    Jan 12, 2015 at 1:23
  • It is recommended (in the documentation) that you do not use use vars above Perl version 5.6. Use our, or preferably my instead.
    – TLP
    Jan 12, 2015 at 3:17
  • you say &main::C() is failing? how?
    – ysth
    Jan 12, 2015 at 3:50
  • @ysth I get a 500 Internal Server Error with a Undefined subroutine &main::C called at /path/to/file.pl line 123 error message in my log.
    – Dayo
    Jan 12, 2015 at 7:31

1 Answer 1

1

%main:ARR or $main::ARR{index} are correct for the code skeleton you have provided (well, anything is correct because you haven't said use strict, but anyway ...). Is it possible that main is not the correct namespace (i.e., is there some pacakge statement that precedes use vars ...) ?

In any case, you can workaround this issue with the our keyword. If you declare it at the top level, it will have scope throughout the rest of the file:

package ABC;
our %ARR;      # %ABC::ARR

sub foo {
   $ARR{"key"} = "value";  # %ABC::ARR
}

{   # "BEGIN" optional
    package XYZ;
    our %Hash;    # %XYZ::Hash
    sub bar {
        my $key1 = $Hash{"key1"};  # %XYZ::Hash
        my $val1 = $ARR{$key1};    # %ABC::ARR
        $ARR{$val1} = $key1;
    }
}

...
1
  • This resolved the issue. The 502 errors I was getting were due to other issues. Thanks!
    – Dayo
    Jan 12, 2015 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.