Set - 2

Question 16 :

What does length(%HASH) produce if you have thirty-seven random keys in a newly created hash?

Answer :

length() is a built-in prototyped as sub length($), and a scalar prototype silently changes aggregates into radically different forms. The scalar sense of a hash is false (0) if it's empty, otherwise it's a string representing the fullness of the buckets, like "18/32" or "39/64". The length of that string is likely to be 5. Likewise, `length(@a)' would be 2 if there were 37 elements in @a.

Question 17 :

If EXPR is an arbitrary expression, what is the difference between $Foo::{EXPR} and *{"Foo::".EXPR}?

Answer :

The second is disallowed under `use strict "refs"'.
Dereferencing a string with *{"STR"} is disallowed under the refs stricture, although *{STR} would not be. This is similar in spirit to the way ${"STR"} is always the symbol table variable, while ${STR} may be the lexical variable. If it's not a bareword, you're playing with the symbol table in a particular dynamic fashion.

Question 18 :

How do I do < fill-in-the-blank > for each element in an array?

Answer :

#!/usr/bin/perl -w
@homeRunHitters = ('McGwire', 'Sosa', 'Maris', 'Ruth');
foreach (@homeRunHitters) {
	print "$_ hit a lot of home runs in one year\n";


Question 19 :

How do I replace every <TAB> character in a file with a comma?

Answer :

perl -pi.bak -e 's/\t/,/g' myfile.txt 


Question 20 :

What is the easiest way to download the contents of a URL with Perl? 

Answer :

Once you have the libwww-perl library, installed, the code is this:

use LWP::Simple;
$url = get '';