• +91 9971497814
  • info@interviewmaterial.com

Computer Interview Questions Answers

Perl Interview Questions Answers

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

Answer - 31 : - 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 - 32 : - Why does Perl not have overloaded functions?

Answer - 32 : - 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 - 33 : - What does read() return at end of file?

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

Question - 34 : - How can I use Perl interactively?

Answer - 34 : - The typical approach uses the Perl debugger, described in the perldebug(1) manpage, on an "empty" program, like this: perl -de 42 Now just type in any legal Perl code, and it will be immediately evaluated. You can also examine the symbol table, get stack backtraces, check variable values, set breakpoints, and other operations typically found in symbolic debuggers.

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

Answer - 35 : - 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 - 36 : - What is Perl one-liner?

Answer - 36 : - There are two ways a Perl script can be run: --from a command line, called one-liner, that means you type and execute immediately on the command line. You'll need the -e option to start like "C:\ %gt perl -e "print \"Hello\";". One-liner doesn't mean one Perl statement. One-liner may contain many statements in one line. --from a script file, called Perl program. Assuming both a local($var) and a my($var) exist, what's the difference between ${var} and ${"var"}? ${var} is the lexical variable $var, and ${"var"} is the dynamic variable $var. Note that because the second is a symbol table lookup, it is disallowed under `use strict "refs"'. The words global, local, package, symbol table, and dynamic all refer to the kind of variables that local() affects, whereas the other sort, those governed by my(), are variously knows as private, lexical, or scoped variable.

Question - 37 : - How do I replace every <TAB> character in a file with a comma?

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

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

Answer - 38 : - 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 - 39 : - How do you print out the next line from a filehandle with all its bytes reversed?

Answer - 39 : - # 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 - 40 : - What happens when you return a reference to a private variable?

Answer - 40 : - Perl keeps track of your variables, whether dynamic or otherwise, and doesn't free things before you're done using them.

Computer Contributors

Rajeev Katiyar
Yes Baroda

Share your email for latest updates


Our partners