PERL interview questions

PERL quiz questions

  • 1.

    What interface used in PERL to connect to database? How do you connect to database in Perl?

    Answer:

    We can connect to database using DBI module in Perl.

    use DBI;
     
    my $dbh = DBI->connect(’dbi:Oracle:orcl’, ‘username’, ‘password’,)

     

    View
  • 2.

    What does the’$_’ symbol mean?

    Answer:

    The ‘$_’ is a default variable in Perl and $_ is known as the “default input and pattern matching space

    View
  • 3.

    Where do we require ‘chomp’ and what does it mean?

    Answer:

    We can eliminate the new line character by using ‘chomp’. It can used in many different scenarios.For example:

    excuteScript.pl FstArgu.
     
    $argu = $ARGV[0];
     
    chomp $argu; --> to get rid of the carrige return.

     

    View
  • 4.

    What is use of ‘->’ symbol?

    Answer:

    In Perl, ‘->’ symbol is an infix dereference operator. if the right hand side is an array subscript, hash key or a subroutine, then the left hand side must be a reference.

    @array = qw/ abcde/; # array
     
    print "n",$array->[0]; # it is wrong
     
    print "n",$array[0]; #it is correct , @array is an array

     

    View
  • 5.

    How can you call a subroutine and identify a subroutine?

    Answer:

    ‘&myvariable’ is used to call a sub-routine and ‘&’ is used to identify a sub-routine.

    View
  • 6.

    “Perl regular expressions match the longest string possible”. What is the name of this match?

    Answer:

    It is called as “greedy match” because Perl regular expressions normally match the longest string possible.

    View
  • 7.

    What are the advantages of c over Perl?

    Answer:

    There are more development tools for C than for PERL. PERL execute slower than C programs. Perl appears to be an interpreted language but the code is complied on the fly. If you don’t want others to use your Perl code you need to hide your code somehow unlike in C. Without additional tools it is impossible to create an executable of a Perl program

    View
  • 8.

    In CPAN module, name an instance you use.

    Answer:

    In CPAN, the CGI and DBI are very common packages

    View
  • 9.

    List the prefix dereferencer in Perl.

    Answer:

    $-Scalar variables

    %-Hash variables

    @-arrays

    &-subroutines

    Type globs-*myvar stands for @myvar, %myvar.

    View
  • 10.

    What do you mean by context of a subroutine?

    Answer:

    It is defined as the type of return value that is expected. You can use a single function that returns different values.

    View
  • 11.

    How can you create anonymous subroutines?

    Answer:

    sub BLOCK
     
    sub PROTO BLOCK
     
    sub ATTRS BLOCK
     
    sub PROTO ATTRS BLOCK

     

    View
  • 12.

    How can memory be managed in Perl?

    Answer:

    Whenever a variable is used in Perl, it occupies some memory space. Since the computer has limited memory the user must be careful of the memory being used by the program. For Example:

    use strict;
     
    open(IN,"in");
     
    my @lines = <IN>
     
    close(IN);
     
    open(OUT,">out");
     
    foreach (@lines)
     
    {
     
    print OUT m/([^\s]+)/,"\n";
     
    }
     
    close(OUT);

    On execution of above program, after reading a file it will print the first word of each line into another file. If the files are too large then the system would run out of memory. To avoid this, the file can be divided into sections.

    View
  • 13.

    Why Perl aliases are considered to be faster than references?

    Answer:

    In Perl, aliases are considered to be faster than references because they do not require any dereferencing.

    View
  • 14.

    Remove the duplicate data from @array=(“perl”,”php”,”perl”,”asp”)

    Answer:

    sub uniqueentr
     
    {
     
    return keys %{{ map { $_ => 1 } @_ }};
     
    }
     
    @array = ("perl","php","perl","asp”);
     
    print join(" ", @array), "\n";
     
    print join(" ", uniqueentr(@array)), "\n";

     

    View
  • 15.

    How can you replace the characters from a string and save the number of replacements?

    Answer:

    #!usr/bin/perl
     
    use strict;
     
    use warnings;
     
    my $string="APerlAReplAFunction";
     
    my $counter = ($string =~ tr/A//);
     
    print "There are $counter As in the given string\n";
     
    print $string;

     

    View
  • 16.

    Which statement has an initialization, condition check and increment expressions in its body? Write a syntax to use that statement.

    Answer:

    for ($count = 10; $count >= 1; $count--)
     
    {
     
    print "$count ";
     
    }

     

    View
  • 17.

    How can you use Perl warnings and what is the importance to use them?

    Answer:

    The Perl warnings are those in which Perl checks the quality of the code that you have produced. Mandatory warnings highlight problems in the lexical analysis stage. Optional warnings highlight cases of possible anomaly.

    use warnings; # it is same as importing "all"
     
    no warnings; # it is same as unimporting "all"
     
    use warnings::register;
     
    if (warnings::enabled()) {
     
    warnings::warn("any warning");
     
    }
     
    if (warnings::enabled("void")) {
     
    warnings::warn("void", "any warning");
     
    }

     

    View
  • 18.

    Write a program that explains the symbolic table clearly.

    Answer:

    In Perl, the symbol table is a hash that contains the list of all the names defined in a namespace and it contains all the functions and variables. For example:

    sub Symbols
     
    {
     
    my($hashRef) = shift;
     
    my(%sym);
     
    my(@sym);
     
    %sym = %{$hashRef};
     
     
    @sym = sort(keys(%sym));
     
    foreach (@sym)
     
    {
     
    printf("%-10.10s| %s\n", $_, $sym{$_});
     
    }
     
    }
     
    Symbols(\%Foo::);
     
    package Foo;
     
    $bar = 2;
     
    sub baz {
     
    $bar++;
     
    }

     

    View
  • 19.

    What is the usage of -i and 0s options?

    Answer:

    The -i option is used to modify the files in-place. This implies that Perl will rename the input file automatically and the output file is opened using the original name. If the -i option is used alone then no backup of the file would be created. Instead -i.bak causes the option to create a backup of the file.

    View
  • 20.

    What is the use of -n and -p options?

    Answer:

    The -n and -p options are used to wrap scripts inside loops. The -n option makes the Perl execute the script inside the loop. The -p option also used the same loop as -n loop but in addition to it, it uses continue. If both the -n and -p options are used together the -p option is given the preference.

    View

© 2017 QuizBucket.org