Perl Programming

  Home  Computer Programming  Perl Programming


“Perl Interview Questions and Answers will guide you that the Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall, a linguist working as a systems administrator for NASA, in 1987, as a general purpose Unix scripting language to make report processing easier. This Perl Interview Questions and Answers Guide will help you to get preparation of job in Perl or learn Pearl by these interview questions and answers.”



46 Perl Programming Questions And Answers

22⟩ 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() );

 135 views

24⟩ 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.

 132 views

25⟩ 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] "stuffn"' 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.

 140 views

26⟩ 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 "ntGRADES IN ASCENDING NUMERIC ORDER:n";

foreach $key (sort hashValueAscendingNum (keys(%grades))) {

print "tt$grades{$key} tt $keyn";

}

print "ntGRADES IN DESCENDING NUMERIC ORDER:n";

foreach $key (sort hashValueDescendingNum (keys(%grades))) {

print "tt$grades{$key} tt $keyn";

}

 146 views

27⟩ 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;

 135 views

28⟩ What is 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.

 149 views

29⟩ 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.

 125 views

31⟩ 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.

 140 views

32⟩ 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.

 182 views

33⟩ 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.

 146 views

34⟩ 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.

 172 views

38⟩ 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";

 130 views

39⟩ When would local $_ in a function ruin your day?

When your caller was in the middle for a while(m//g) loop

The /g state on a global variable is not protected by running local on it. That'll teach you to stop using locals. Too bad $_ can't be the target of a my() -- yet.

 127 views

40⟩ 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";

}

 134 views