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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:11:34+00:00 2026-06-07T06:11:34+00:00

I have been USEing .pm files willy-nilly in my programs without really getting into

  • 0

I have been USEing .pm files willy-nilly in my programs without really getting into using packages unless really needed. In essence, I would just have common routines in a .pm and they would become part of main when use’d (the .pm would have no package or Exporter or the like… just a bunch of routines).

However, I came across a situation the other day which I think I know what happened and why, but I was hoping to get some coaching from the experts here on best practices and how to resolve this. In essence, should packages be used always? Should I “do” files when I just want common routines absorbed into main (or the parent module/package)? Is Exporter really the way to handle all of this?

Here’s example code of what I came across (I won’t post the original code as it’s thousands of lines… this is just the essence of the problem).

Pgm1.pl:

use PM1;
use PM2;
print "main\n";
&pm2sub1;
&pm1sub1;

PM1.pm:

package PM1;
require Exporter;
@ISA=qw(Exporter);
@EXPORT=qw(pm1sub1);
use Data::Dump 'dump';
use PM2;
&pm2sub1;
sub pm1sub1 {
    print "pm1sub1 from caller ",dump(caller()),"\n";
    &pm2sub1;
}
1;

PM2.pm:

use Data::Dump 'dump';
sub pm2sub1 {
    print "pm2sub1 from caller ",dump(caller()),"\n";
}
1;

In essence, I’d been use’ing PM2.pm for some time with its &pm2sub1 subroutine. Then I wrote PM1.pm at some point and it needed PM2.pm’s routines as well. However, in doing it like this, PM2.pm’s modules got absorbed into PM2.pm’s package and then Pgm1.pl couldn’t do the same since PM2.pm had already been use’d.

This code will produce

Undefined subroutine &main::pm2sub1 called at E:\Scripts\PackageFun\Pgm1.pl line 4.
pm2sub1 from caller ("PM1", "PM1.pm", 7)
main

However, when I swap the use statements like so in Pgm1.pl

use PM2;
use PM1;
print "main\n";
&pm2sub1;
&pm1sub1;

… perl will allow PM2.pm’s modules into main, but then not into PM1.pm’s package:

Undefined subroutine &PM1::pm2sub1 called at PM1.pm line 7.
Compilation failed in require at E:\Scripts\PackageFun\Pgm1.pl line 2.
BEGIN failed--compilation aborted at E:\Scripts\PackageFun\Pgm1.pl line 2.

So, I think I can fix this by getting religious about packages and Exporter in all my modules. Trouble is, PM2.pm is already used in a great number of other programs, so it would be a ton of regression testing to make sure I didn’t break anything.

Ideas?

  • 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-07T06:11:35+00:00Added an answer on June 7, 2026 at 6:11 am

    use will not load same file more than once. It will, however, call target package’s import sub every time. You should format your PM2 as proper package, so use can find its import and export function to requestor’s namespace from there.

    (Or you could sneak your import function into proper package by fully qualifying its name, but don’t do that.)

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

Sidebar

Related Questions

I have been using Sandcastle Help File Builder to produce help files (HTML 1.0)
How can you find out what transaction log backup files have been restored using
I have been using TortoiseSVN for some time and I really like it. I
I have been using a product which displays parsed XML files as HTML. It
I have been using fop 0.95 to generate pdf files from xml data. I
I have been using Apache POI to manipulate Microsoft Word .docx files — ie
I'm relatively new to Vim and have been using it without issues so far.
I have been using emacs for some time now and am slowly getting the
Recently I have been using alot of text files (csv) with 10-60k lines, something
I have been using JDOM library to read and write XML files through Java

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.