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 8770833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:38:16+00:00 2026-06-13T17:38:16+00:00

so, i have a program like this: #!/usr/bin/perl -w use strict; foreach (@data) {

  • 0

so, i have a program like this:

#!/usr/bin/perl -w  
use strict;  
foreach (@data) {  
    if($_ eq "foo") {  
        use Foo;
        process();
    }  
    if($_ eq "bar") {  
        use Bar;
        process();
    }  
...  
}

Each included module is somewhat similar, the only difference being what the process()-sub does.

#!/usr/bin/perl -w  
use strict;  
sub process {  
...  
}

My issue: the input for the main script is a (possibly long) list of things, while processing that list i get continuous “Subroutine redefined” errors (obviously). Is there any way to “un-use” a module?
Due to the fact that the library of possible “actions” to include may grow in the future, i thought this way of including modules dynamically would be the best approach. Many thanks for your help 🙂

  • 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-06-13T17:38:17+00:00Added an answer on June 13, 2026 at 5:38 pm

    As @pilcrow said, you can solve this issue quickly by using require instead of use, but, I think this is a good example where Polymorphism is to be used.

    You can create a base class like:

    package Processors::Base;
    
    sub new{
      my $class = shift;
      return bless {}, $class;
    }
    
    sub process{
      die "You can only use a subclass of me";
    }
    
    1;
    

    And then, create your processors as packages that inherit from this base package.

    package Processors::Foo;
    
    sub process{
      ... do stuff ...
    }
    
    1;
    

    Then your code could look like:

    #!/usr/bin/perl -w  
    use strict;  
    for my $pkg (@data) { 
        (my $path = $pkg) =~ s{::}{/}g;
        require "$path.pm";
        $pkg->process; 
        ...  
    }
    

    Of course, the modifications assume that $_ is in the form of Processors::Foo for example. Even if you can not modify the content of your @data, I think you can generate the name of the processor so that you are able to invoke its process() method.

    If you want to be a show-off, you could create a Factory object that will return instances of your processors based on the value of $_:

    package Processors::Factory;
    
    sub get_instance{
      my ($self, $processor_name) = @_;
    
      my $full_processor_name = sprintf('Processors::%s', ucfirst($processor_name) );
    
      (my $full_processor_path = $full_processor_pkg) =~ s{::}{/}g;
      require "$full_processor_path.pm";
    
      my $processor = $full_processor_name->new();
    
      return $processor;
    }
    
    1;
    

    Then, your code would look like:

    #!/usr/bin/perl -w  
    use strict;
    use Processors::Factory;
    
    foreach (@data) { 
        Processors::Factory->get_instance( $_ )->process();
        ...  
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table like this: Application,Program,UsedObject It can have data like this: A,P1,ZZ
I wrote a test program that looked like this: #!/usr/bin/python def incrementc(): c =
I have a code like this: #!/usr/bin/env bash test_this(){ export ABC=ABC echo some output
I have a program like this - import weka.core.stemmers.SnowballStemmer; public class TestProject{ public static
I have a program that looks like this. I need to consistently write something
I have a program that looks something like this: public partial class It {
I have a program with three threads. I call them like this: if __name__
I have a program that simulates mouse click. Code is something like this: [DllImport(user32.dll,
I'm just learning to program...I have a dropdownlist like this. <div> <asp:DropDownList ID=DropDownList1 runat=server>
I have a piece of code like this: Class Program { static StreamReader sr

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.