Saturday, 9 November 2024

Easiest Ways to Install Missing Perl Modules

When you encounter an error like Can't locate Foo.pm in @INC, it means Perl couldn’t find a required module. Fortunately, there are various ways to install Perl modules, each suited to different needs and environments. Here’s a guide to some of the most convenient ways to install missing Perl modules.

1. Installing Modules with CPAN

CPAN (Comprehensive Perl Archive Network) is Perl’s standard tool for installing modules. Here’s how to use it from the command line.

Read more »

Labels:

Sunday, 2 February 2025

Which Version of Perl Should I Use on Windows?

 When it comes to using Perl on Windows, there are two main distributions to choose from: ActivePerl and Strawberry Perl. Both have their own advantages, and the choice largely depends on your specific needs and preferences. Let’s explore the differences between these two popular Perl distributions to help you decide which is the best fit for your environment.

ActivePerl: A Long-Standing Favorite

ActivePerl, provided by ActiveState, has been the go-to Perl distribution for Windows users for many years. It’s often chosen by enterprise environments and developers who value stability and ease of deployment. Here are some key features:

Read more »

Labels:

Thursday, 21 November 2024

Creating ETL Pipeline using Perl DBD::SQlite

Prerequisites

  1. Perl: Ensure Perl is installed on your system.
  2. CPAN Modules: Install the necessary CPAN modules:
    • Text::CSV_XS for CSV handling.
    • DBD::SQLite for SQLite database interaction.

You can install these modules using CPAN:

cpan Text::CSV_XS
cpan DBD::SQLite

ETL Pipeline Script

#!/usr/bin/perl
use strict;
use warnings;
use Text::CSV_XS;
use DBI;

# Configuration
my $input_csv  = 'input_data.csv';
my $output_db  = 'output_data.db';
my $table_name = 'transformed_data';

# Step 1: Extract - Read data from CSV
sub extract_data {
    my $csv = Text::CSV_XS->new({ binary => 1, auto_diag => 1 });
    open my $fh, "<:encoding(utf8)", $input_csv or die "Cannot open $input_csv: $!";

    my @data;
    while (my $row = $csv->getline($fh)) {
        push @data, $row;
    }
    close $fh;

    return \@data;
}

# Step 2: Transform - Perform data transformation
sub transform_data {
    my ($data) = @_;
    my @transformed_data;

    foreach my $row (@$data) {
        # Example transformation: Convert all names to uppercase
        my ($id, $name, $age) = @$row;
        $name = uc($name);

        push @transformed_data, [$id, $name, $age];
    }

    return \@transformed_data;
}

# Step 3: Load - Insert transformed data into SQLite database
sub load_data {
    my ($data) = @_;

    # Connect to SQLite database
    my $dbh = DBI->connect("dbi:SQLite:dbname=$output_db", "", "", { RaiseError => 1, AutoCommit => 1 })
        or die "Could not connect to database: $DBI::errstr";

    # Create table if it doesn't exist
    $dbh->do("CREATE TABLE IF NOT EXISTS $table_name (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)");

    # Prepare insert statement
    my $sth = $dbh->prepare("INSERT INTO $table_name (id, name, age) VALUES (?, ?, ?)");

    # Insert each row
    foreach my $row (@$data) {
        $sth->execute(@$row);
    }

    # Disconnect from database
    $dbh->disconnect();
}

# Main ETL Process
sub main {
    my $extracted_data = extract_data();
    my $transformed_data = transform_data($extracted_data);
    load_data($transformed_data);

    print "ETL process completed successfully!\n";
}

# Run the ETL pipeline
main();

Explanation

  1. Extract:

    • The extract_data function reads data from a CSV file using the Text::CSV_XS module.
    • It reads each row and stores it in an array of arrays.
  2. Transform:

    • The transform_data function takes the extracted data and performs transformations.
    • In this example, it converts all names to uppercase.
  3. Load:

    • The load_data function connects to an SQLite database using the DBD::SQLite module.
    • It creates a table if it doesn’t exist and inserts the transformed data into the table.
  4. Main:

    • The main function orchestrates the ETL process by calling the extract, transform, and load functions in sequence.

Running the Script

  1. Save the script to a file, e.g., etl_pipeline.pl.
  2. Make the script executable:
    chmod +x etl_pipeline.pl
    
  3. Run the script:
    ./etl_pipeline.pl
    

Sample Input CSV (input_data.csv)

id,name,age
1,John Doe,30
2,Jane Smith,25
3,Alice Johnson,35

Expected Output in SQLite Database

After running the script, the output_data.db SQLite database will contain a table transformed_data with the following data:

id name age
1 JOHN DOE 30
2 JANE SMITH 25
3 ALICE JOHNSON 35

This Perl script provides a basic yet complete ETL pipeline that can be easily extended or modified to suit more complex scenarios. It demonstrates how to extract data from a CSV file, transform it, and load it into a SQLite database. This example should serve as a useful reference for developers looking to implement ETL processes in Perl.

Labels:

Wednesday, 21 August 2024

Entering the World of Perl: Interactive Perl Consoles

 

Perl, a powerful and versatile scripting language, is widely used for system administration, web development, network programming, and more. Unlike some other scripting languages like Python and Ruby, Perl does not come with an interactive shell by default. However, Perl’s flexibility allows for various options to set up an interactive console, enhancing learning and debugging processes.
Read more »

Labels:

Tuesday, 6 August 2019

Can't locate Foo.pm in @INC, install it using cpan and solve it in many methods?


Method1:
cpan File::Name

Method2:
in windows: 
ppm
ppm> search net-smtp
ppm> install Net-SMTP-Multipart

Method3:
sudo perl -MCPAN -e 'install Foo'

Method4:
start cpan in your shell:
# cpan
and type
install File::Name

Tuesday, 10 January 2023

How to Upgrade CPAN in Latest versions using perl?

 


Method1:
 
perl -MCPAN -e "upgrade /(.\*)/"

Method2:
 
in cpan shell type "cpan upgrade /(.*)/"

Method3:
 
cpan-outdated -p | cpanm

Method4:
 
cpanm App::cpanoutdated

Labels: , , , ,

Tuesday, 5 November 2019

perl - 5 ways cpan module installation


Method 1:(Manual Installation From cpan.org)

use wget  or manual search  from  metacpan.org
and get the package download path and use with wget or download manually.

Extract it as described below,

tar -xzvf XML-Simple-2.25.tar.gz
cd XML-Simple-2.25

perl Makefile.PL->make->make test ->make install


Read more »

Labels: , ,

Thursday, 30 January 2025

Building an ETL Pipeline with Perl and Amazon Redshift

Creating an ETL pipeline that interacts with a data warehouse (e.g., Amazon Redshift, Google BigQuery, Snowflake, etc.) is a common use case in modern data engineering. In this blog post, we’ll walk through building an ETL pipeline in Perl that extracts data from a data warehouse, transforms it, and loads it into another data warehouse or database. For this example, we’ll use Amazon Redshift as the data warehouse.

Overview

This ETL pipeline will:

  1. Extract: Fetch data from an Amazon Redshift data warehouse.
  2. Transform: Perform transformations on the data (e.g., cleaning, aggregations, or calculations).
  3. Load: Insert the transformed data into another Amazon Redshift table or a different data warehouse.
Read more »

Labels: