Forum Moderators: coopster & phranque

Message Too Old, No Replies

$main::Counter{myvar1} Perl notation - what does it mean?

$main::Counter{myvar1} vs. main::mysub(myvar2)

         

randallxski

2:34 pm on Jul 7, 2004 (gmt 0)

10+ Year Member



I'm having some trouble understanding some Perl. I have some references to main::mysubroutine(myvariable) that calls mysubroutine in the primary Perl file. I also have references to something using the notation $main::Counter{myvariable} that's a bit confusing.

What does $main::Counter{myvariable} do? I can't find any references to Counter or myvariable in any of the Perl files.

SeanW

10:55 pm on Jul 7, 2004 (gmt 0)

10+ Year Member



The main:: specifies the variable/sub is in the main package (ie your script). So main::foo() calls the foo function, $main::foo{bar} refers to the bar element of the foo hash.

I've never found a need to use main::, though I've gone the other way around (ie to access a package's variables from main). Chances are you might be in a package, ie

[perl]
print $foo; # undef
Bar::quux();
print $foo; # should be 1
package Bar;

sub quux {
$main::foo = 1;
}
[/perl]

(that should work)

Sean