PERL TUTORIALS - Perl Variables
Perl Variables
ADVERTISEMENTS
Variable Context
S.N. | Context and Description |
---|---|
1 | Scalar: Assignment to a scalar variable evaluates the right-hand side in a scalar context. |
2 | List: Assignment to an array or a hash evaluates the right-hand side in a list context. |
3 | Boolean: Boolean context is simply any place where an expression is being evaluated to see whether it's true or false |
4 | Void: This context not only doesn't care what the return value is, it doesn't even want a return value. |
5 | Interpolative: This context only happens inside quotes, or things that work like quotes. |
ADVERTISEMENTS
Creating Variables
$age = 25; # An integer assignment $name = "John Paul"; # A string $salary = 1445.50; # A floating pointADVERTISEMENTSScalar Variables
#!/usr/bin/perl $age = 25; # An integer assignment $name = "John Paul"; # A string $salary = 1445.50; # A floating point print "Age = $age\n"; print "Name = $name\n"; print "Salary = $salary\n";Array Variables
#!/usr/bin/perl @ages = (25, 30, 40); @names = ("John Paul", "Lisa", "Kumar"); print "\$ages[0] = $ages[0]\n"; print "\$ages[1] = $ages[1]\n"; print "\$ages[2] = $ages[2]\n"; print "\$names[0] = $names[0]\n"; print "\$names[1] = $names[1]\n"; print "\$names[2] = $names[2]\n";Hash Variables
#!/usr/bin/perl %data = ('John Paul', 45, 'Lisa', 30, 'Kumar', 40); print "\$data{'John Paul'} = $data{'John Paul'}\n"; print "\$data{'Lisa'} = $data{'Lisa'}\n"; print "\$data{'Kumar'} = $data{'Kumar'}\n";Variable Context
#!/usr/bin/perl @names = ('John Paul', 'Lisa', 'Kumar'); @copy = @names; $size = @names; print "Given names are : @copy\n"; print "Number of names are : $size\n";