Monday 30 September 2019

How to assign more than one arrays to crate a complete hash in perl?


Method 1[Faster]:


                     %final=();
                    @first=(7,8,3,4);
                     @second=('a','s','d','f');
                     my %final;
                    @final{@first} = @second;
                     print %final;
Read more »

Labels: , , , , , ,

Tuesday 1 October 2019

hash-inversion-by-reversing-the-values-to-keys-and-keys-to-values-in-perl


Method 1:

                             %hash=(9,5,2,1,4,6);
                             @final5{values %hash} = keys %hash;
                              print @final5;


Method 2:

                              $z=0;
                              %hash=(9,5,2,1,7,6);
                              foreach((sort values %hash)) {
                              $final5{ $_ } = (sort keys %hash)[$z];
                              $z++;
                              }


Method 3:

                                %hash=(9,5,2,1,4,6);

                                @first1=values %hash;
                                @second2=keys %hash;
                                foreach my $a1 (@first1)
                                {
                                        my $a2= shift @second2;
                                       $final3{ $a1 } = $a2;
                                 }


Method 4:

                               %hash=(9,5,2,1,4,6);
                               @first=values %hash;
                               @second=keys %hash;
                               for($j = 0; $j < @first; $j++)
                               {
                                         $final{$first[$j]} = $second[$j];
                               }


Method 5:

                                %hash=(9,5,6,1);
                               @first_1=values %hash;
                               @second_1=keys %hash;
                               while( @first_1 || @second_1)
                              {
                                      my $a1= shift @first_1 || "Perl_Methods_Master";
                                      my $a2= shift @second_1 || "Perl_Methods_Master";
                                      $final{$a1} = $a2;
                               }



Method 6:

                                %hash=(9,5,2,1,4,6);
                               @first1=values %hash;
                               @second2=keys %hash;
                               while( @first1 && @second2)
                              {
                                      my $b1= shift @first1;
                                      my $b2= shift @second2;
                                      $final{$b1} = $b2;
                               }



Method 7:

                               %hash=(9,5,2,1,4,6);
                               @first2=values %hash;
                                @second1=keys %hash;
                               while (@first2 || @second1)
                               {
                                           $final{shift @first2} = shift @second1;
                                }


Method  8:

                               %hash=(9,5,2,1,4,6);
                               @first1=values %hash;
                               @second1=keys %hash;
                               $final{shift @first1}=shift(@second1) while @second1;



Method 9:

                             %final1=(9,5,2,1,4,6);
                             @first2=values %final1;
                             @second2=keys %final1;
                              $i=0;
                             %final2=map {$first[$i++],$_; } @second2;



Method 10:

                             %final5=(9,5,2,1,4,6);
                              while (($k, $v) = each %final5)
                             {
                                        $final2{$v}=$k;
                              }



Method 11: 
                   
                              %final5=(9,5,2,1,4,6);
                              %final5 = reverse %final5;

                      .

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

Saturday 12 November 2022

perl rest client crud tutorial example using REST::Client


Hi,Today  Perl script that demonstrates how to interact with a REST API using the REST::Client module. The script shows examples of performing HTTP requests such as GET, POST, and DELETE to interact with different endpoints of a RESTful API. The script starts by importing necessary modules, and then sets up basic configurations for authentication and API endpoint URLs. The code defines three subroutines, each performing a specific type of API request: GetList(), CreatePost(), and DeleteList(). These subroutines use the HTTP methods of the REST::Client module to call the respective API endpoints, and they return the API response. Finally, the script calls these subroutines and prints the API responses on the console.

Read more »

Labels: , , ,

Friday 9 October 2020

perl webservices mojolicious tutorial

Perl provides several libraries and frameworks for building web services. Here is a brief tutorial on how to build a web service using Perl:

1.Choose a Perl framework or library for web services. Some popular choices include:

  • Mojolicious
  • Dancer2
  • Catalyst
  • Plack
  • CGI

2.Define your web service's API. This involves deciding on the endpoints that your service will expose, as well as the request and response formats. For example, you might define an endpoint for retrieving a list of users, with a JSON response format.

Read more »

Labels: , ,

Tuesday 22 October 2019

place and plays @_ in perl?

Method 1:

sub Addition{ $a=\@_; ($a,$b,$c)=@$a; $sum=$a+$b+$c; } Addtion(10, 20, 30); print $sum; #60



Read more »

Labels:

Thursday 24 October 2019

bless - place and plays in perl

WITHOUT   BLESS                                      WITH BLESS
                                                                       
                                                           package Class;
package Class;                                   sub new  { $classname = @_; 

sub new  { $classname = @_;                 $var = {};
$var= {};                                              bless($var, $classname);
return $var; }                                         return $var; }
sub sample { print("TEST"); }       sub sample{ print("TEST"); }
$var1 = Class->new();                       $var1 = Class->new(); 
$o->sample();                                        $o->sample();    
                                               
    
Output:                                                          Output:
Error                                                                TEST
            



Method 1:

sub new { 
  $classvar = shift; 
  $selfref = {};  
  bless $selfref, $classvar; 
}


Method 2:

sub new
{
$a={};
return bless $a;
}


Method 3:

sub new
{
$a={};
bless($a);
return $a;
}


Method 4:

sub new
{
$classvar = shift;
$a={};
bless($a,$classvar);
return $a;
}

Method 5:

sub new
{
$classname=shift;
$a={};
$a->{NAME}="apple";
bless $a,$classname;
}

Method 6:

sub new
{
$classname=shift;
$a=[];
$a->[0]=100;
bless $a,$classname;
return $a;
}

Method 7:

my $x = bless [], "packagename";
$x->functionname;

Method 8:

sub new {
    classvar = $_[0];
    my $var = $_[1]->{'name'};
 
    my $this = {
        'name' => $var,
    }; 
    bless($this, $classvar);
    return $this;
}

Method 9:

 $var1= "scalar";
$var2 = bless \$s, $classname;
return $var2;


Method 10:

my $obj = bless($ref, "Class");
    sub bless {
      my ($ref, $class) = @_;
      $ref->{classname} = $class;
      return $ref;
    }

Method 11:


sub new { 
  $classname = shift; 
  $self = [];  
  bless $self, $classname; 
}


Method 12:

sub new
{
$a=[];
return bless $a;
}

Friday 11 December 2020

Perl - securing web services using OAuth 2.0


Hi, This program is a simple OAuth 2.0 authentication and authorization implementation using the Mojolicious web framework in Perl. It allows a user to log in and authorize a client application to access protected resources.

The program defines the necessary endpoints for authentication and authorization using the get method from the Mojolicious::Lite module. The $auth_endpoint and $token_endpoint variables define the authentication and token endpoints for the OAuth 2.0 provider. The $client_id, $client_secret, $redirect_uri, and $scope variables define the client credentials and scope for the OAuth 2.0 client.

Read more »

Labels: