1

I cannot seem to create a variable that is global to that class and usable in all subroutines of that class.

I see examples all over that apparently work, but I cannot get anything I do to work.

Code:

my $test = new Player(8470598);

package Player;

use strict;
use warnings;
use Const::Fast;


$Player::URL = 'asdfasdasURL';
my $test3 = '33333333';
our $test4 = '44444444444';
const $Player::test0 => 'asdfasdas000';
const my $test1 => 'asdfasdas111';
const our $test2 => 'asdfasdas222';

sub new{
    print $Player::URL;
    print $Player::test0;
    print $test1;
    print $test2;
    print $test3;
    print $test4;
    return(bless({}, shift));
}

Output:

Use of uninitialized value $Player::URL in print at D:\Text\Programming\Hockey\test.pl line 19.
Use of uninitialized value $Player::test0 in print at D:\Text\Programming\Hockey\test.pl line 20.
Use of uninitialized value $test1 in print at D:\Text\Programming\Hockey\test.pl line 21.
Use of uninitialized value $Player::test2 in print at D:\Text\Programming\Hockey\test.pl line 22.
Use of uninitialized value $test3 in print at D:\Text\Programming\Hockey\test.pl line 23.
Use of uninitialized value $Player::test4 in print at D:\Text\Programming\Hockey\test.pl line 24.

What is going on here?

2 Answers 2

4

While the entire code will be compiled before any is executed, the executable parts happen in order. In particular, your new() call happens before any of the assignments or const calls in package Player.

Moving all the Player code to a Player.pm file and invoking it with use Player; will cause it to be immediately compiled and executed before the new and work as you expect.

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

5 Comments

An alternative would be to initialize (but not declare) the variables inside an INIT or BEGIN block.
OH, seems to have fixed it. That is a strange order of execution. So the class variables are not executed until execution gets to the class. This is wrong behaviour, right? Perl should execute the free code in a package before starting normal execution, right? being able to call a method in a class before that class is even fully created is just strange.
you can make it do that, by moving the code to a separate file and useing it, or by enclosing it in a BEGIN block. the code isn't really "in" a package; all the package statement does is change the default package for any unqualified identifiers from that point to the next package or end of the enclosing block or file (and change what __PACKAGE__ and SUPER:: refer to)
@JonathonWisnoski: Statements are executed in the order they appear in the file. The only difference a package statement makes is to change the default namespace of any package variables (declared with our). While it is possible to have multiple package statements in a single source file it can be difficult to get things working, and the proper way is to put each module in a *.pm file with the same name as its package, and use it from the source that needs it. At the very least you should put the main package last in the program file so that any initialisation is done first.
my $test = new Player(8470598); is free code code in a package too!
0

Package level code

my $test = new Player(8470598);

is being executed before package level code

$Player::URL = 'asdfasdasURL';
my $test3 = '33333333';
our $test4 = '44444444444';
const $Player::test0 => 'asdfasdas000';
const my $test1 => 'asdfasdas111';
const our $test2 => 'asdfasdas222';

because it comes earlier in the file.

If you want to inline a package, change

use Player;

to

BEGIN {
    package Player;

    ...

    $INC{"Player.pm"} = 1;
}

use Player;
my $test = Player->new(8470598);

You can sometimes cut corners. In this case, you can cut both:

  • BEGIN isn't needed.
  • $INC{"Player.pm"} = 1; can be dropped by dropping use Player;.

Comments

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.