What is the "my" keyword used for in PERL?

Recommended Answers

All 2 Replies

It sets scope:

my $var1="world";
{
	my $var1="hello";
	print "$var1 ";
}
print "$var1\n";

Output:

hello world

It sets scope:

my $var1="world";
{
	my $var1="hello";
	print "$var1 ";
}
print "$var1\n";

Output:

hello world

Good answer. If you want to read more I strongly recommend Coping With Scoping which explains in simple language with examples the difference between variables declared with my and package, or global, variables.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.