Set - 1

Question 6 :

How do I do fill_in_the_blank for each file in a directory?

Answer :

Here's code that just prints a listing of every file in the current directory:

#!/usr/bin/perl -w
opendir(DIR, ".");
@files = readdir(DIR);
closedir(DIR);
foreach $file (@files) {
	print "$file\n";
}

 


Question 7 :

How do I generate a list of all .html files in a directory?

Answer :

Here's a snippet of code that just prints a listing of every file in the current directory that ends with the extension .html:

#!/usr/bin/perl -w
opendir(DIR, ".");
@files = grep(/\.html$/,readdir(DIR));
closedir(DIR);
foreach $file (@files) {
	print "$file\n";
}

 


Question 8 :

What is Perl one-liner?

Answer :

There are two ways a Perl script can be run:
--from a command line, called one-liner, that means you type and execute immediately on the command line. You'll need the -e option to start like "C:\ %gt perl -e "print \"Hello\";". One-liner doesn't mean one Perl statement. One-liner may contain many statements in one line.
--from a script file, called Perl program.


Question 9 :

Assuming both a local($var) and a my($var) exist, what's the difference between ${var} and ${"var"}? 

Answer :

${var} is the lexical variable $var, and ${"var"} is the dynamic variable $var.
Note that because the second is a symbol table lookup, it is disallowed under `use strict "refs"'. The words global, local, package, symbol table, and dynamic all refer to the kind of variables that local() affects, whereas the other sort, those governed by my(), are variously knows as private, lexical, or scoped variable.


Question 10 :

What happens when you return a reference to a private variable?

Answer :

Perl keeps track of your variables, whether dynamic or otherwise, and doesn't free things before you're done using them.