Loginskip to content

October 30th, 2006

What’s a symbolic reference? It’s when a scalar

What’s a symbolic reference? It’s when a scalar contains a string that names another variable, and you attempt to use that scalar as though it were a hard reference. This works, if you’re not using strict or have selectively disabled the component of use strictthat prevents it.[3] [3] You do this with the statement no strict ‘refs’. Now, in Perl 4, storing the name of a variable inside another variable was the only way to carry out certain very common tasks (and required that you also use eval). But since the advent of hard references in Perl 5, there is almost no situation in which you will need to use a symbolic reference. However, there may be many more occasions when you think that you need to. So let’s examine some of those. The program that follows is a hypothetical greengrocer’s inventory counter that reads data consisting of a fruit or vegetable and the number of cases bought and sold of that item, separated by spaces on new lines.[4] [4] Notice that we defied our own indenting style to put the if statement blocks on one line each, and left out the semicolon after their sole statements. This just goes to show that sometimes there’s no need to be a slave to style if breaking the rules buys you an advantage like greater readability. 1 $fruit_cases_bought = 0; 2 $fruit_cases_sold = 0; 3 $vegetables_cases_bought = 0; 4 $vegetables_cases_sold = 0; 5 %vegetable = map { ($_, 1) } qw(carrots broccoli 6 kale spinach leeks); 7 %fruit = map { ($_, 1) } qw(apples kumquats 8 pears bananas kiwis); 9 while () 10 { 11 ($food, $cases_bought, $cases_sold) = split; 12 if (exists $vegetable{$food}) { $type = ‘vegetable’ } 13 elsif (exists $fruit{$food}) { $type = ‘fruit’ } 14 else { warn “I don’t know the food type $foodn” } 15 16 if (defined $type) 17 { 18 ${$type.’_cases_bought’} += $cases_bought; 19 ${$type.’_cases_sold’} += $cases_sold; 20 } 21 } 22 23 print $vegetable_cases_bought - $vegetable_cases_sold, 24 ” cases of vegetables on handn”; 25 26 print $fruit_cases_bought - $fruit_cases_sold, 27 ” cases of fruit on handn”;
Note: If you are looking for top 10 and very good webhost to host and run your jsp application check Actions jsp hosting services

Comments are closed.