Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3353854
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:12:07+00:00 2026-05-18T02:12:07+00:00

I have 2 sub s in my perl code that are all but identical.

  • 0

I have 2 subs in my perl code that are all but identical. I’m curious as to how SO would refactor them to make one sub.

The only real difference in them is a regex, and a query via prepared statement. The prepared statements take different parameters as well.

Thoughts?

sub showcaseViewsSubData {
    my ($api_call, $stat_section, $idsite, $prev_date, $last_of_month, $params, $subtable) = @_;

    return unless ($subtable);

    my %sub_params = %{ clone ($params) };
    $sub_params{'idSubtable'} = $subtable->{'idsubdatatable'};

    # $data contains views for each primary showcase page
    my $data = &fetchStatsData($api_call, \%sub_params);

    foreach my $visit_group (@$data) {

        # ignore product pages
        next if ($visit_group->{'url'} && $visit_group->{'url'} =~ /\/products?\//);

        # if ($visit_group->{'url'} && $visit_group->{'url'} =~ /inthenews|pressreleases|downloads/) {
        if ($visit_group->{'idsubdatatable'}) {
            &showcaseViewsSubData($api_call, $stat_section, $idsite, $prev_date, $last_of_month, $params, $visit_group);
            next;
        }

        my $division_name;

        if ($visit_group->{'url'}) {
            my $showcase_id = $visit_group->{'url'};
            $showcase_id =~ s/^.*?\/(\d+)\/.*$/$1/;

            $division_by_showcase_id_sth->execute($showcase_id);
            ($division_name) = $division_by_showcase_id_sth->fetchrow_array();

        } else {
            $visit_group->{'orig_label'} = $visit_group->{'label'};
            $visit_group->{'label'} =~ s/-/%/g;
            $visit_group->{'label'} =~ s|^/||g;
            $visit_group->{'label'} .= '%';
            $division_sth->execute($visit_group->{'label'});
            ($division_name) = $division_sth->fetchrow_array();
        }

        if ($division_name) {

            ## no idea why this is nb_hits, and not nb_actions, like every other method
            my @data_value = ( { 'nb_actions' => ($visit_group->{'nb_hits'} || $visit_group->{'nb_visits'}), 'label' => $division_name } );
            &updateCompanyStats($idsite, 'showcase', $prev_date, \@data_value);
        }
    }
    return 1;
}

and

sub researchViewsSubData {
    my ($api_call, $stat_section, $idsite, $prev_date, $last_of_month, $params, $subtable) = @_;

    return unless ($subtable);

    my %sub_params = %{ clone ($params) };
    $sub_params{'idSubtable'} = $subtable->{'idsubdatatable'};

    # $data contains views for each primary showcase page
    my $data = &fetchStatsData($api_call, \%sub_params);

    if (ref $data ne 'ARRAY') {
        carp "$api_call returned something not an array!";
        return 0;
    }

    foreach my $visit_group (@$data) {
        # if ($visit_group->{'url'} && $visit_group->{'url'} =~ /inthenews|pressreleases|downloads/) {
        if ($visit_group->{'idsubdatatable'}) {
            &researchViewsSubData($api_call, $stat_section, $idsite, $prev_date, $last_of_month, $params, $visit_group);
            next;
        }

        my $division_name;

        if ($visit_group->{'url'}) {
            my $tag_id = $visit_group->{'url'};
            $tag_id =~ s/^.*?\/(\d+)\/.*$/$1/;

            next if ($tag_id !~ /^\d+$/);

            $division_by_tag_id_sth->execute(int($tag_id), int($idsite));
            ($division_name) = $division_by_tag_id_sth->fetchrow_array();

        } else {
            carp Dumper($visit_group);
        }

        if ($division_name) {
            ## no idea why this is nb_hits, and not nb_actions, like every other method
            my @data_value = ( { 'nb_actions' => ($visit_group->{'nb_hits'} || $visit_group->{'nb_visits'}), 'label' => $division_name } );

            &updateCompanyStats($idsite, 'research', $prev_date, \@data_value);
        }
    }
    return 1;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T02:12:07+00:00Added an answer on May 18, 2026 at 2:12 am

    As what Sinan said, you should start from scratch and design a new class or module (or set of them) for dealing with the functionality you need.

    Below is something that I produced based on your code. It is meant just as an outline to give you a direction.

    package MyCompanyStats;
    
    sub process_stats {
        my $params = shift;
        my $data = fetchStatsData($params);
        foreach my $visit_group (@$data) {
            process_stats_group($visit_group);
        }
    }
    
    sub process_stats_group {
        my $group = shift;
        if ($group->{'url'}) {
            my $showcase_id = get_showcase_id($group);
            save_by_showcase_id($showcase_id, $group);
        } else {
            my $group_label = get_group_label($group);
            save_by_label($group_label, $group);
        }
    }
    
    sub get_group_label {
        my $group = shift;
        my $label = $group->{'label'};
        for ($label) {
            s/-/%/g;
            s|^/||g;
        }
        $label .= '%';
        return $label;
    }
    
    sub get_showcase_id {
        my $group = shift;
        my $showcase_id = $visit_group->{'url'};
        $showcase_id =~ s/^.*?\/(\d+)\/.*$/$1/;
        return $showcase_id;
    }
    
    1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a domain and a group of sub-domains that require authentication to access.
I have the following code in my class : sub new { my $class
I have a WinForms TreeView with one main node and several sub-nodes. How can
I have image data and i want to get a sub image of that
I have classX: Sub New(ByVal item_line_no As String, ByVal item_text As String) ' check
i have a sub with this signature Public Sub Save(ByVal obj As IPta) now
I'm developing a website which to begin with will have three clear sub sites:
I have a folder FolderA which contains three sub-folders: foldera1 foldera2 and foldera3 I
I have a main form and as sub form. I need the main Form
I have a multi-module maven project with an installer sub-project. The installer will be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.