• +91 9723535972
  • info@interviewmaterial.com

Perl Interview Questions and Answers

Perl Interview Questions and Answers

Question - 21 : - How do you print out the next line from a filehandle with all its bytes reversed?

Answer - 21 : - # the rest of your program is up here ... open(MAIL, "|/usr/lib/sendmail -t"); print MAIL "To: $sendToAddress\n"; print MAIL "From: $myEmailAddress\n"; print MAIL "Subject: $subject\n"; print MAIL "This is the message body.\n"; print MAIL "Put your message here in the body.\n"; close (MAIL);

Question - 22 : - How to read from a pipeline with Perl

Answer - 22 : - 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 = ; 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 () { ($uid,$pid,$ppid,$restOfLine) = split; # do whatever I want with the variables here ... } close(PS_F);

Question - 23 : - Why is it hard to call this function: sub y { "because" } ?

Answer - 23 : - 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].

Question - 24 : - What does `$result = f() .. g()' really return?

Answer - 24 : - 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() );

Question - 25 : - How do I send e-mail from a Perl/CGI program on a Unix system?

Answer - 25 : - Sending e-mail from a Perl/CGI program on a Unix computer system is usually pretty simple. Most Perl programs directly invoke the Unix sendmail program. We'll go through a quick example here. Assuming that you've already have e-mail information you need, such as the send-to address and subject, you can use these next steps to generate and send the e-mail message:

Question - 26 : - What does read() return at end of file?

Answer - 26 : - 0 A defined (but false) 0 value is the proper indication of the end of file for read() and sysread().

Question - 27 : - How do I sort a hash by the hash value? 

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

Question - 28 : - How to dereference a reference?

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

Question - 29 : - What does length(%HASH) produce if you have thirty-seven random keys in a newly created hash? 

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

Question - 30 : - How do you give functions private variables that retain their values between calls?

Answer - 30 : - Create a scope surrounding that sub that contains lexicals. Only lexical variables are truly private, and they will persist even when their block exits if something still cares about them. Thus: { my $i = 0; sub next_i { $i++ } sub last_i { --$i } } creates two functions that share a private variable. The $i variable will not be deallocated when its block goes away because next_i and last_i need to be able to access it.


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners