Thursday 21 October 2021

write 1000 uniq perl interview questions and answers to master in perl part 2

 251. Q: In Perl, how can you find the position of a substring within a string?

1A: In Perl, you can find the position of a substring within a string by using the `index` function. Here is an example: 2 3```perl 4use strict; 5use warnings; 6 7my $string = 'Hello, World! World is beautiful.'; 8my $substring = 'World'; 9 10my $position = index $string, $substring; 11 12if ($position == -1) { 13 print "The substring was not found in the string.\n"; 14} else { 15 print "The substring was found at position: $position\n"; 16} 17``` 18 19In this example, the script finds the position of the substring `'World'` within the string `'Hello, World! World is beautiful.'` and prints the resulting position.

Read more »

Labels:

Wednesday 13 November 2019

perl sort keys - hash alphabetically


%hashmap=('awk','perl','code','things','sed','need');

Method 1:[alphabetically]


foreach $keys (sort keys %hashmap) {

    print "$food is $hashmap{$keys}.\n";

}


Method 2:[Sort Based on Value]
foreach $keys (sort { $hashmap{$a} cmp $hashmap{$b} }

                keys %hashmap)

{

    print "$keys is $hashmap{$keys}.\n";

}

Method 3:[sort Based on value Length]
@length = sort { length($hashmap{$a}) <=> length($hashmap{$b}) } keys %hashmap;

foreach $keys (@length) {

    print "$keys is $hashmap{$keys}.\n";

}

kaavannan perl blogspot

Labels: , ,

Friday 4 October 2019

how many types available for getting string variable length in perl?

Method 1:

my $c = map $_, $h =~ /(.)/g;


Method 2:

my $c = split '', $h;


Method 3:

my $count = () = "hello" =~ /./g;


Method 4:

print length $h;


Method 5:

$cnt = "hello" =~ tr/[a-z][A-Z][0-9]//;


Method 6:

$h='hello';
my $v=$h=~ s/[a-z]//g;


Method 7:

print  @_ = push(@_, map $_, (split '', 'hellgo'));


Method 8:

print  @_ = push(@_, grep $_, (split '', 'hellgo'));


Method 9:

print push(@_, grep $_, "hello" =~ /./g);


Method 10:

print push(@_, map $_, "hello" =~ /./g);


Method 11:

print push(@_, grep $_, reverse "hello" =~ /./g);


Method 12:

print push(@_, map $_, reverse "hello" =~ /./g);


Method 13:

print push(@_, map $_, (split '', 'hellgo'));


Method 14:


my $c = grep/./, (reverse split '', 'hello');
print $c;


Method 15:

my $c = map $_, (reverse split '', 'hello');
print $c;


Method 16:

my $c = map $_, (reverse "hello" =~ /./g);
print $c;


Method 17:

my $c = grep/./, (reverse "hello" =~ /./g);
print $c;


Method 18:

my $c = map $_, (reverse split '', 'hello');
print $c;


Method 19:

my $c = grep/./, (reverse split '', 'hello');
print $c;


Method 20:

print scalar map $_, 'hello' =~ /(.)/g;


Method 21:

print scalar grep/./,'helloh' =~ /(.)/g;


Method 22:

print scalar  map $_, (reverse "hello" =~ /./g);


Method 23:

print scalar  map $_, (reverse split '', 'hello');


Method 24:

print scalar  map $_, (split '', 'hello');


Method 25:

print $r = @_= map $_, (split '', 'hello');


Method 26:

print $r = @_= grep $_, (split '', 'hello');


Method 27:

print $r = @_= grep $_, (reverse "hello" =~ /./g);


Method 28:

print $r = @_= grep/./,'helloh' =~ /(.)/g


Method 29:

print $r = @_= map  $_, (reverse "hello" =~ /./g);


Method 30:

print $r = @_= map/./,'helloh' =~ /(.)/g


Method 31:

print $r = @_= map $_, (split '', 'hello');


Method 32:

print $r = @_= grep $_, (split '', 'hello');


Method 33:

print $r = @_= grep $_, (reverse "hello" =~ /./g);


Method 34:

print $r = @_= grep/./,'helloh' =~ /(.)/g


Method 35:

print $r = @_= map  $_, (reverse "hello" =~ /./g);


Method 36:

print $r = @_= map/./,'helloh' =~ /(.)/g


Method 37:

print scalar  map $_, (reverse split '', 'hello');


Method 38:

print length reverse  grep/./,'helloh' =~ /(.)/g;


Method 39:

print length reverse  map $_,'helloh' =~ /(.)/g;


Method 40:

print grep/./, (reverse "hello" =~ tr/[a-z][A-Z][0-9]//);


Method 41:

print map $_, (reverse "hello" =~ tr/[a-z][A-Z][0-9]// );




For More Methods Reach Me:@ letscrackperlinterviewblogspot@gmail.com  

Labels: , , , , ,

Thursday 28 October 2021

Top 500 unique perl programming interview questions and answers to master in perl regular expression programs - part1

 

  1. How can I remove leading and trailing white spaces from a string in Perl?
perl
1$str = " Hello World! "; 2$str =~ s/^\s+|\s+$//g; 3print $str; # Output: "Hello World!"
Read more »

Labels:

Thursday 9 December 2021

Perl secure web services using Input validation

 Input validation is a crucial security measure for protecting Perl web services against common attacks, such as injection attacks and cross-site scripting (XSS) attacks. In Perl, you can use various modules and functions to implement input validation and ensure that user input is safe and valid before processing it.

Here's an example code snippet that demonstrates how to implement input validation in Perl using the Data::Validate module:

Read more »

Labels:

Monday 5 August 2019

How to pass command line Arguments to a perl script using multiple methods?


Method1:

perl tmp.pl 1 2 7 3 5
($a,$bcsa,$msx, $flag ,$key) = @ARGV;

Method2:

while (my $var = <>) {
  print $var;
}

Method3:

while (my $var1 = shift) {
  print "$var1\n";
}

Method6:
foreach my $argnum (0 .. $#ARGV) {

   print "$ARGV[$argnum]\n";

}

Method7:
foreach my $arg (@ARGV) {
    print $arg, "\n";
}

Method5:

If the arguments are options/switches, use GetOpt::Std or GetOpt::Long,
If they have a different meaning, you can use GetOpt::Std and GetOpt::Long to process them easily.

 GetOpt::Std supports only single-character switches and GetOpt::Long is much more flexible. From GetOpt::Long:

use Getopt::Long;
my $data   = "file.dat";
my $length = 24;
my $verbose;
$result = GetOptions ("length=i" => \$length,    # numeric
                    "file=s"   => \$data,      # string
                    "verbose"  => \$verbose);  # flag
Alternatively, @ARGV is a special variable that contains all the command line arguments. $ARGV[0] is the first (ie. "string1" in your case) and $ARGV[1] is the second argument. You don't need a special module to access @ARGV.

kaavannan perl blogspot

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: , , ,

Sunday 24 November 2019

Perl - without reverse function reverse a string

Perl Reverse:
perl reverse function reverses the string which will be very useful for creating or while writing the large code. it saves the time from writing new custom functions for reverse.


Without Perl Reverse Function we can write our custom functions in Perl as below

Read more »

Labels: ,