• +91 9723535972
  • info@interviewmaterial.com

Perl Interview Questions and Answers

Perl Interview Questions and Answers

Question - 31 : - How many ways can we express string in Perl?  

Answer - 31 : - Many. For example 'this is a string' can be expressed in: "this is a string" qq/this is a string like double-quoted string/ qq^this is a string like double-quoted string^ q/this is a string/ q&this is a string& q(this is a string)

Question - 32 : - What does `new $cur->{LINK}' do? (Assume the current package has no new() function of its own.)    

Answer - 32 : - $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.

Question - 33 : - How do I do < fill-in-the-blank > for each element in an array?

Answer - 33 : - #!/usr/bin/perl -w @homeRunHitters = ('McGwire', 'Sosa', 'Maris', 'Ruth'); foreach (@homeRunHitters) { print "$_ hit a lot of home runs in one year\n"; }

Question - 34 : - How do I replace every character in a file with a comma?

Answer - 34 : - perl -pi.bak -e 's/\t/,/g' myfile.txt

Question - 35 : - What is the easiest way to download the contents of a URL with Perl?

Answer - 35 : - 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/';

Question - 36 : - How to concatenate strings with Perl?

Answer - 36 : - 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";

Question - 37 : - Why does Perl not have overloaded functions?

Answer - 37 : - 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.

Question - 38 : - How do I read command-line arguments with Perl?

Answer - 38 : - 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"; }

Question - 39 : - When would `local $_' in a function ruin your day?

Answer - 39 : - 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.

Question - 40 : - What happens to objects lost in "unreachable" memory, such as the object returned by Ob->new() in `{ my $ap; $ap = [ Ob->new(), \$ap ]; }' ?  

Answer - 40 : - Their destructors are called when that interpreter thread shuts down. When the interpreter exits, it first does an exhaustive search looking for anything that it allocated. This allows Perl to be used in embedded and multithreaded applications safely, and furthermore guarantees correctness of object code. Assume that $ref refers to a scalar, an array, a hash or to some nested data structure. Explain the following statements: $$ref; # returns a scalar $$ref[0]; # returns the first element of that array $ref- > [0]; # returns the first element of that array @$ref; # returns the contents of that array, or number of elements, in scalar context $&$ref; # returns the last index in that array $ref- > [0][5]; # returns the sixth element in the first row @{$ref- > {key}} # returns the contents of the array that is the value of the key "key"


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners