0

Consider the following code snippet:

#GLOBAL SPACE HERE
{
    #NESTED HERE
    my %hash = (
        key1 => 'A',
        key2 => 'B',
        key3 => 'C',
        key4 => 'D',
    );

    sub test
    {
        #subroutine code goes here
    }
}

How would I access either the hash or the subroutine from the global scope when they are nested within the curly braces?

1 Answer 1

2

The hash is lexically scoped to the block (the curly braces), so it can only be accessed from inside that block. The subroutine can be accessed from anywhere, regardless of scope.

Sign up to request clarification or add additional context in comments.

4 Comments

the variable is declared at compile time too; the difference is that subroutines are not lexically scoped.
@ysth The variable cannot be accessed until runtime execution has reached the point in the code where it is declared. The subroutine is available at all points in the code at runtime, regardless of scope.
re your first point, no: my $x; BEGIN { use strict; $x = 42 } print $x. re your second point, yes, that's what I'm saying you should say instead of "as it is declared at compile time", the latter being not the issue.
@ysth Well, that's peculiar.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.