Subtle things in perl
There are some untold things in Perl, that really caught my attention. Firstly, we can assign an array to a scalar variable. Doing so will initialize that scalar to the size of that array. There is also an explicit function for this. Here is what I am talking about:
@arr = qw(i can be a bad guy);
$var = @arr;
print “$var”;
This will print 6. In the same way we can also use an inbuilt function for this. The function is called ’scalar’
$var = scalar(@arr); assigns the size of the array. In the same way we can also initialize a variable to a list. But the variable will get initialized to the last variable of that list. For example:
$var = (1,2,3,4); will initialize $var to 4.
July 13, 2008 at 3:25 pm
Great Information !
Thank you for keeping up the good work. I look forward to returning to your blog, and learning more from you !