Friday 2 October 2020

write 1000 uniq perl interview questions and answers to master in perl - part1

  1. Q: In Perl, what does the sigil $_ represent?

    A: In Perl, the sigil $_ represents the default argument to a subroutine. It is a global variable that is set automatically by Perl whenever a subroutine is called.

  2. Q: In Perl, what does the backslash \ do in front of a variable name?

    A: In Perl, the backslash \ in front of a variable name is used to force a string interpretation of the variable, rather than a numeric interpretation. This can be useful when working with non-numeric variables, or when you want to avoid potential side effects caused by Perl's built-in operations on numbers

Read more »

Labels:

Sunday 8 March 2020

Perl - Backtick Tutorial

backticks are used to execute shell commands and capture the output. The general syntax for using backticks in Perl is:

$output = `command`;


Here, the command is enclosed in backticks, and the output is captured into the variable $output.

For example, if you want to capture the output of the ls command, you can use:

$files = `ls`;

The variable $files will now contain the output of the ls command.

You can also use variables in the command that is enclosed in backticks:

$dir = "/path/to/directory";

$files = `ls $dir`;

Here, the value of the $dir variable is used in the ls command.

Backticks can also be used in combination with other Perl functions to process the output. For example, you can split the output into an array:

$files = `ls`;

@file_list = split(/\n/, $files);

Here, the split function is used to split the output of the ls command into an array, with each element containing a file name.

Backticks can also be used in conditional statements. For example, you can check if a file exists using the test command:

$file = "myfile.txt";

if (`test -e $file`) {

    print "$file exists\n";

} else {

    print "$file does not exist\n";

}

Here, the test command is enclosed in backticks and used in the conditional statement to check if the file exists.

It's important to note that using backticks to execute shell commands can be a security risk if the input is not properly sanitized. Make sure to validate and sanitize any user input before using it in a shell command.

Labels:

Thursday 17 March 2022

how to atomically update values in a ConcurrentHashMap in Java using Compute and ComputeIfAbsent methods?

Hi,  ConcurrentHashMap is a thread-safe implementation of the Java Map interface, designed to be used in multi-threaded environments. It allows multiple threads to access and modify the map simultaneously without causing data corruption or race conditions.

One of the key features of ConcurrentHashMap is that it provides atomic operations to update its elements. Atomic operations are operations that are performed as a single, indivisible unit of work, which means they are guaranteed to be executed completely or not at all. This is important in multi-threaded environments where multiple threads may try to modify the same element at the same time.

Read more »

Labels: , ,

Sunday 15 January 2023

Perl Interview Questions and Answers - March 2023 Updated 2

 Find Lowest Missing Numbers in perl

The given program is a Perl script designed to find the lowest missing numbers from an array of integers. The script begins by initializing an array of integers named "@arr", which contains a set of random integers. The script then creates a hash called "%hash" that maps each integer in "@arr" to the value 1.

Next, the script finds the maximum integer in "@arr" using the "sort" function and saves it to a variable named "$max". It also sets the value of the minimum integer to -1, and initializes an empty array called "@missing" to store the missing numbers.

Read more »

Labels:

Tuesday 1 October 2019

how to get verified the hash and its elements availability in perl?

Method 1:

                          %final=(9,5,8,1,4,7);
                          print "not empty string" if exists($final{"keys"});

Method 2:

                          %final=(9,5,8,1,4,7);
                          if(delete $final{9})
                         {
                              print "empty deleted";
                          }
                          else
                         {
                              print "not exits";
                          }


Method  3:
                     
                          %final=(9,5,8,1,4,7);
                          if(%final)
                         {
                              print "string";
                          }
                          else
                         {
                              print "empty";
                          }


Method  4:

                           %final=(9,5,8,1,4,7);
                           if(!%final)
                         {
                              print "empty";
                          }
                          else
                         {
                              print "available";
                          }

kaavannan perl blogspot

Labels: ,

Friday 1 November 2019

perl - find missing numbers in array

Method 1:

@a = (1,3,4,6); @c = map $a[$_-1]+1..$a[$_]-1, 1..@a-1; print join(" ", @c);




Read more »

Labels:

Saturday 11 March 2023

How to Build an Authentication API with JWT Token in Perl

Hi, In many web applications, user authentication is a critical feature that allows users to securely log in and access their data. One common approach to implementing authentication is to use JSON Web Tokens (JWT), which are a type of token-based authentication that can be used across multiple domains and platforms.

In this tutorial, we will show you how to build an authentication API with JWT token in Perl. We will be using the Mojolicious web framework, which is a powerful and flexible framework that makes it easy to build web applications in Perl.

Read more »

Labels: , , ,

Tuesday 7 March 2023

Perl Code for Gmail Validator



To validate a Gmail address using regular expressions in Perl, you can use the following code:


#!/usr/bin/perl

use strict;

use warnings;

sub is_valid_gmail {

    my $email = shift;

    # Regular expression pattern for Gmail address validation

    my $pattern = qr/^[a-zA-Z0-9._%+-]+@gmail\.com$/;

    if ($email =~ $pattern) {

        return 1;  # Valid Gmail address

    } else {

        return 0;  # Invalid Gmail address

    }

}

# Testing the function

my $email1 = 'example@gmail.com';

my $email2 = 'invalid_email@gmail.com';


if (is_valid_gmail($email1)) {

    print "$email1 is a valid Gmail address\n";

} else {

    print "$email1 is not a valid Gmail address\n";

}


if (is_valid_gmail($email2)) {

    print "$email2 is a valid Gmail address\n";

} else {

    print "$email2 is not a valid Gmail address\n";

}


This code defines a Perl subroutine is_valid_gmail that takes an email address as input and checks if it is a valid Gmail address. The regular expression pattern ^[a-zA-Z0-9._%+-]+@gmail\.com$ is used to match the email address against the Gmail format.

To test the function, two email addresses are provided: example@gmail.com and invalid_email@gmail.com. The function is called for each email address, and the result is printed to the console.

Please note that this code only validates the format of the email address and checks if it ends with @gmail.com. It does not verify if the email address actually exists or is currently in use.

Labels: