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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:17:54+00:00 2026-05-13T17:17:54+00:00

Help Perlers! Does anyone know a simple insert code here approach would bring code

  • 0

Help Perlers! Does anyone know a simple “insert code here” approach would bring code from external files once at compile time into a package?

Background:

I have a standard PBP-style inside-out class that is getting quite large. I want to split the code into multiple files but not extend the class. Optimally, I would simply insert the code into the “Magic JuJu” section of the example module (see below) once at compile time.

I have looked at AutoLoader as a means to get this accomplished. However, there are two things that give me pause. If I could get around these, it might be a near-optimal solution:

  1. I don’t want to split every little sub into a separate file; just a few more reasonably sized files (using sub stubs in the caller is fine, though); and

  2. I don’t want to defer compile on every sub; some subs I’d like to have compiled on initial use. However, this is probably not a deal breaker.

I know Moose provides “Roles”, which I believe does this well, but for various reasons, Moose is not an option for this project, nor is Mouse.

I have used “require q(some/file)” in the “Magic JuJu” location, but that does not maintain the persistent variable scope, i.e. subs from the external file don’t “see” the object attribute hashes correctly (in still other words, putting the require at the top of the file would have the same effect). I could get around that by always using setters and getters. So that is not a deal breaker, but would require a bit of coding time and execution overhead that I’d rather not incur.

Finally, I don’t want to extend the class; it already has multiple inheritance. I just want a simple “insert code here” approach would bring the code in once at compile time.

In summary:

  1. (Required) imports code from external files into package namespace
  2. (Required) Does so only at compile-time or minimal run-time overhead
  3. (Required) Does not extend class
  4. (Desired) Honors insert location scope

Example Code with “Magic JuJu” comment below:

package T;

use strict;
use warnings;

########## BEGIN object persistent variables scope block ############
{
  my %Attr_Name_Env;

  ## Constructor 'new'
  #
  sub new {
    ## Get and confirm arguments
    #
    my $class      = shift;
    my $href_arg = {@_};
    my $name_env = $href_arg->{'name_env'};

    ## Bless anon scalar into class
    #
    my $obj_new   = bless anon_scalar(), $class;
    my $idx_self  = ident $obj_new;

    # Populate object attributes
    #
    $Attr_Name_Env{ $idx_self } = $name_env;

    return $obj_new;
  }
  ## END Constructor 'new'

  sub DESTROY {... as you do ...}

  sub t_get_name_env {
    my $self      = shift;
    my $idx_self  = ident $self;
    return $Attr_Name_Env{ $idx_self };
  }

  ## insert magic juju here

}
########## END object persistent variables scope block ############

1;

Maybe a BEGIN block with a slurp and eval …

  • 1 1 Answer
  • 2 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-13T17:17:54+00:00Added an answer on May 13, 2026 at 5:17 pm

    You could just use the modules and import the desired subs.

    use MyMod qw( wanted_sub_1 wanted_sub2 );
    

    To get access to your attribute hashes you will need to modify import for your target modules.

    package MyClass;
    use MyMod { attrib1 => \%attrib1, attrib2 => \%attrib2 }, qw( wanted_sub1 wanted_sub2 );
    

    MyMod‘s import would then create code refs that are closed over the initial hash argument, and install those into the MyClass namespace.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Help me pleas. I need to find some way to open files with external
Help, I can't seem to identify the problem in my code below. Here's what
Help me translate following block of the Haskell code. The run function produces text
Help from the SQL Gods Required Table 1 - DistinctQuotes This tables holds information
help out a noob with a simple web development question?? I want to create
Help me to find best approach for managing Backbone views. For example I have
HELP......Just moved to C# from vb and Im really lost with this. var ldapmembershipUser
Help for this specific server side php code I don't have any knowledge of
Help me with this part of code: class Ooo attr_accessor :class_array end def func(ctx)
Help, can anyone give me any pointers as too how and install StarTeam 2006

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.