|
Technical Interview Questions
Python Interview Questions
C++ Interview Questions
Php Interview Questions
Xml
Interview Questions
C Interview Questions
.........More
Soft Skills
Communication Skills
Leadership Skills
.........More
|
|
Perl Interview Questions and Answers
How to read from a pipeline with Perl
Example 1:
To run the date command from a Perl program, and read
the output
of the command, all you need are a few lines of code
like this:
open(DATE, "date|");
$theDate = <DATE>;
close(DATE);
The open() function runs the external date command, then
opens
a file handle DATE to the output of the date command.
Next, the output of the date command is read into
the variable $theDate through the file handle DATE.
Example 2:
The following code runs the "ps -f" command, and reads
the output:
open(PS_F, "ps -f|");
while (<PS_F>) {
($uid,$pid,$ppid,$restOfLine) = split;
# do whatever I want with the variables here ...
}
close(PS_F);
Why is it hard to call this function: sub y { "because"
}
Because y is a kind of quoting operator.
The y/// operator is the sed-savvy synonym for tr///.
That means y(3) would be like tr(), which would be
looking for a second string, as in tr/a-z/A-Z/,
tr(a-z)(A-Z), or tr[a-z][A-Z].
What does `$result = f() .. g()' really return?
False so long as f() returns false, after which it
returns true until g() returns true, and then starts the
cycle again.
This is scalar not list context, so we have the bistable
flip-flop range operator famous in parsing of mail
messages, as in `$in_body = /^$/ .. eof()'. Except for
the first time f() returns true, g() is entirely
ignored, and f() will be ignored while g() later when
g() is evaluated. Double dot is the inclusive range
operator, f() and g() will both be evaluated on the same
record. If you don't want that to happen, the exclusive
range operator, triple dots, can be used instead. For
extra credit, describe this:
$bingo = ( a() .. b() ) ... ( c() .. d() );
Why does Perl not have overloaded functions?
Because you can inspect the argument count, return
context, and object types all by yourself.
In Perl, the number of arguments is trivially available
to a function via the scalar sense of @_, the return
context via wantarray(), and the types of the arguments
via ref() if they're references and simple pattern
matching like /^\d+$/ otherwise. In languages like C++
where you can't do this, you simply must resort to
overloading of functions.
What does read() return at end of file?
0
A defined (but false) 0 value is the proper indication
of the end of file for read() and sysread().
What does `new $cur->{LINK}' do? (Assume the current
package has no new() function of its own.)
$cur->new()->{LINK}
The indirect object syntax only has a single token
lookahead. That means if new() is a method, it only
grabs the very next token, not the entire following
expression.
This is why `new $obj[23] arg' does't work, as well as
why `print $fh[23] "stuff\n"' does't work. Mixing
notations between the OO and IO notations is perilous.
If you always use arrow syntax for method calls, and
nothing else, you'll not be surprised.
How do I sort a hash by the hash value?
Here's a program that prints the contents
of the grades hash, sorted numerically by the hash
value:
#!/usr/bin/perl -w
# Help sort a hash by the hash 'value', not the 'key'.
to highest).
sub hashValueAscendingNum {
$grades{$a} <=> $grades{$b};
}
# Help sort a hash by the hash 'value', not the 'key'.
# Values are returned in descending numeric order
# (highest to lowest).
sub hashValueDescendingNum {
$grades{$b} <=> $grades{$a};
}
%grades = (
student1 => 90,
student2 => 75,
student3 => 96,
student4 => 55,
student5 => 76,
);
print "\n\tGRADES IN ASCENDING NUMERIC ORDER:\n";
foreach $key (sort hashValueAscendingNum (keys(%grades)))
{
print "\t\t$grades{$key} \t\t $key\n";
}
print "\n\tGRADES IN DESCENDING NUMERIC ORDER:\n";
foreach $key (sort hashValueDescendingNum (keys(%grades)))
{
print "\t\t$grades{$key} \t\t $key\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
|