|
Perl Interview Questions and Answers
How to read file into hash array ?
open(IN, "<name_file")
or die "Couldn't open file for processing: $!";
while (<IN>) {
chomp;
$hash_table{$_} = 0;
}
close IN;
print "$_ = $hash_table{$_}\n" foreach keys %hash_table;
How do you find the length of an array?
$@array
What value is returned by a lone `return;' statement?
The undefined value in scalar context, and the empty
list value () in list context.
This way functions that wish to return failure can just
use a simple return without worrying about the context
in which they were called.
What's the difference between /^Foo/s and /^Foo/?
The second would match Foo other than at the start of
the record if $* were set.
The deprecated $* flag does double duty, filling the
roles of both /s and /m. By using /s, you suppress any
settings of that spooky variable, and force your carets
and dollars to match only at the ends of the string and
not at ends of line as well -- just as they would if $*
weren't set at all.
Does Perl have reference type?
Yes. Perl can make a scalar or hash type reference by
using backslash operator.
For example
$str = "here we go"; # a scalar variable
$strref = \$str; # a reference to a scalar
@array = (1..10); # an array
$arrayref = \@array; # a reference to an array
Note that the reference itself is a scalar.
How to dereference a reference?
There are a number of ways to dereference a reference.
Using two dollar signs to dereference a scalar.
$original = $$strref;
Using @ sign to dereference an array.
@list = @$arrayref;
Similar for hashes.
What does length(%HASH) produce if you have thirty-seven
random keys in a newly created hash?
5
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.
If EXPR is an arbitrary expression, what is the
difference between $Foo::{EXPR} and *{"Foo::".EXPR}?
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.
How do I do < fill-in-the-blank > for each element in an
array?
#!/usr/bin/perl -w
@homeRunHitters = ('McGwire', 'Sosa', 'Maris', 'Ruth');
foreach (@homeRunHitters) {
print "$_ hit a lot of home runs in one year\n";
}
How do I replace every <TAB> character in a file with a
comma?
perl -pi.bak -e 's/\t/,/g' myfile.txt
What is the easiest way to download the contents of a
URL with Perl?
Once you have the libwww-perl library, LWP.pm installed,
the code is this:
#!/usr/bin/perl
use LWP::Simple;
$url = get 'http://www.websitename.com/';
How to concatenate strings with Perl?
Method #1 - using Perl's dot operator:
$name = 'checkbook';
$filename = "/tmp/" . $name . ".tmp";
Method #2 - using Perl's join function
$name = "checkbook";
$filename = join "", "/tmp/", $name, ".tmp";
Method #3 - usual way of concatenating strings
$filename = "/tmp/${name}.tmp";
How do I read command-line arguments with Perl?
With Perl, command-line arguments are stored in the
array named @ARGV.
$ARGV[0] contains the first argument, $ARGV[1] contains
the second argument, etc.
$#ARGV is the subscript of the last element of the @ARGV
array, so the number of arguments on the command line is
$#ARGV + 1.
Here's a simple program:
#!/usr/bin/perl
$numArgs = $#ARGV + 1;
print "thanks, you gave me $numArgs command-line
arguments.\n";
foreach $argnum (0 .. $#ARGV) {
print "$ARGV[$argnum]\n";
}
Page Numbers :
1
2
3
4
5
Have a Question ?
post your questions here. It
will be answered as soon as possible.
Check
HTML Interview
Questions for more HTML Interview Questions with Answers
Check
Job Interview Questions
for more Interview Questions with Answers
|