Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does perl recognise end of variable while printing?

Tags:

perl

How does perl recognise end of variable?

For example, this code:

use warnings;
my $a = 10;
print "Value of a is $a:::";

Output:

Use of uninitialized value $a:: in concatenation (.) or string at tryprint.pl line 6.
Value of a is :

Why does it consider $a:: and not $a: or $a:::

This works:

print "Value of a is $a\:::";

prints:

Value of a is 10:::
like image 405
rv1822 Avatar asked Mar 25 '23 06:03

rv1822


1 Answers

:: is used to print/access a variable from a package/symbol table. For eg, to access a scalar variable x in package abc, Perl uses $abc::x where abc is the name of the symbol table and x is the variable. Similarly, when you used $a:::, Perl thought there is a package whose name is 'a' and the variable name as :, and hence those error.

See this example below:

our $a = 10;
{
        my $a=20;
        print "lexical a is $a \n";
        print "Value of a is $main::a";
}
like image 80
Guru Avatar answered Apr 01 '23 01:04

Guru