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.

Advertisements

One Response to “Subtle things in perl”

  1. Great Information !
    Thank you for keeping up the good work. I look forward to returning to your blog, and learning more from you !

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: