Вы находитесь на странице: 1из 3

1) Why should I use the -w argument with my Perl programs?

Many Perl developers use the -w option of the interpreter, especially during the
development stages of an application. This warning option turns on many warning
messages that can help you understand and debug your applications.
To use this option on Unix systems, just include it on the first line of the pro
gram, like this:
#!/usr/bin/perl -w
If you develop Perl apps on a DOS/Windows computer, and you're creating a progra
m named myApp.pl, you can turn on the warning messages when you run your program
like this:
perl -w myApp.pl
2) What is the meaning of $, @ and % preceding a variable?
$ is a scalar. @ is an array. % is a hash.
3) Give me an example of adding a value to an array.
push @array, "value"
or
$array[@array] = "value"
4) What function would you use to prepend a value to an array?
unshift
5) For a variable named array how would I determine the last index?
$#array
6) What function do I use to test whether a hash key is present?
exists
7) What function do I use to retrieve the next key/value pair from a hash?
each
8) What function do I use to raise an exception or bail out?
die
9) What function do I use to return lower-case version of a string?
lc
10) What function removes a value from a hash?
delete
11) What function will remove a trailing record separator from a string?
chomp
12) What function will create an object?
bless
13) What function will remove the first element of an array, and return it?
shift
14) What function will remove the last element from an array and return it?
pop
15) How do I set environment variables in Perl programs?
you can just do something like this:
$ENV{'PATH'} = '...';
As you may remember, "%ENV" is a special hash in Perl that contains the value of
all your environment variables.
Because %ENV is a hash, you can set environment variables just as you'd set the
value of any Perl hash variable. Here's how you can set your PATH variable to ma
ke sure the following four directories are in your path::
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin:/home/yourname/bin';
16) How to open and read data files with Perl
Data files are opened in Perl using the open() function. When you open a data fi
le, all you have to do is specify (a) a file handle and (b) the name of the file
you want to read from.
As an example, suppose you need to read some data from a file named "checkbook.t
xt". Here's a simple open statement that opens the checkbook file for read acces
s: open (CHECKBOOK, "checkbook.txt"); In this example, the name "CHECKBOOK" is t
he file handle that you'll use later when reading from the checkbook.txt data fi
le. Any time you want to read data from the checkbook file, just use the file ha
ndle named "CHECKBOOK".
Now that we've opened the checkbook file, we'd like to be able to read what's in
it. Here's how to read one line of data from the checkbook file:
$record = < CHECKBOOK > ;
After this statement is executed, the variable $record contains the contents of
the first line of the checkbook file. The "<>" symbol is called the line reading
operator.
To print every record of information from the checkbook file
open (CHECKBOOK, "checkbook.txt") || die "couldn't open the file!";
while ($record = < CHECKBOOK >) {
print $record;
}
close(CHECKBOOK);
17) What is Perl one-liner?
There are two ways a Perl script can be run:
--from a command line, called one-liner, that means you type and execute immedia
tely 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.
18) How do you find the length of an array?
$@array or scalar(@array)
19) What does is the meaning of $_ ?
Default variable in Perl.
20) Difference between exec & system?
exec runs the given process, switches to its name and never returns while system
forks off the given process, waits for it to complete and then returns.
21) How do I print the entire contents of an array with Perl?
print "@array"
or
foreach (@array) { print $_ }
or
foreach (@array) { print }
or
map { print } @array

Вам также может понравиться