Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Subroutines not exporting? Why?

by no21 (Sexton)
on Jul 06, 2015 at 14:06 UTC ( [id://1133361]=perlquestion: print w/replies, xml ) Need Help??

no21 has asked for the wisdom of the Perl Monks concerning the following question:

I have to be doing something dumb to not realize what the problem is here.

I have a script with the following:

use lib '/home/username/testmodules/'; use strict; use warnings; use My::Util; my x = get_file_name( ... );

The following file exists:

/home/username/testmodules/My/Util.pm

The Util.pm file basically contains the following:

package My::Util; use strict; use warnings; use Exporter; our @ISA = qw(Exporter); our @EXPORT = qw( get_file_name ); __END__ sub get_file_name { ... } 1;

When I run the script, I get:

Undefined subroutine &My::Util::get_file_name called

I've googled this problem and found many, many pages discussing this very topic, and from everything I can tell I'm doing things correctly.

It's acting like the subroutines from the Util.pm module are not being exported, but I am using other modules successfully and (I appear to be) exporting their functions the exact same way.

Please expose my ignorance. I MUST be missing something.

Replies are listed 'Best First'.
Re: Subroutines not exporting? Why?
by BrowserUk (Patriarch) on Jul 06, 2015 at 14:13 UTC

    How do you expect a subroutine that is after the __END__ token to ever be compiled, let alone exported?

    __END__ sub get_file_name { ... }

    This looks like a throwback to the bad old days of AutoSplit -- which was a hinky trick at the best of times, and these days really doesn't make sense -- but if you really have a good reason to defer compilation, then you would need to be using the AutoSplit module.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
    I'm with torvalds on this Agile (and TDD) debunked I told'em LLVM was the way to go. But did they listen!

      Thank you so much for your time and your help. You absolutely nailed it.

      In an attempt to test some changes I wanted to make to an existing custom module that is using AutoLoader and AutoSplit, I just copied it to a different directory and made some changes. I removed the...

      use AutoLoader; *AUTOLOAD = \&AutoLoader::AUTOLOAD;

      But I never removed the...

      __END__

      Thanks again. I really appreciate the help. Is there some way I can give you guys "credit" for helping me? I see the "++" radio buttons. Is that what I should use? I don't post to perlmonks very often, so that's why I'm asking.

        Upvoting (++) is a very appropriate way to show appreciation to all who offer useful advice.


        Give a man a fish:  <%-(-(-(-<

Re: Subroutines not exporting? Why?
by stevieb (Canon) on Jul 06, 2015 at 14:16 UTC

    Well, one glaring problem I see is that you have your sub definition after __END__ which I'm sure won't work. Try removing __END__, or at least put the code above it.

    From perldoc perldata: "The two control characters ^D and ^Z, and the tokens __END__ and __DATA__ may be used to indicate the logical end of the script before the actual end of file. Any following text is ignored.".

    -stevieb

Re: Subroutines not exporting? Why?
by toolic (Bishop) on Jul 06, 2015 at 14:41 UTC
    A good editor with syntax highlighting will make it obvious that the code after __END__ is essentially commented out.

      Good point.

      I'm using vim and it was trying to help me exactly as you stated (code appearing commented out), but my ignorance overlooked it.

Re: Subroutines not exporting? Why?
by neilwatson (Priest) on Jul 06, 2015 at 14:16 UTC

    Lose the end as suggested. See a tutorial by our own: here.

    UPDATE fixed URL

    Neil Watson
    watson-wilson.ca

Re: Subroutines not exporting? Why?
by marinersk (Priest) on Jul 06, 2015 at 14:27 UTC

    I suspect you are using __END__where what you really meant was exit;.

    1. First perlhas to read your script file and produce the under-the-covers code it will execute; this is sometimes called "compilation" and sometimes "interpretation" (the latter is more technically correct);
    2. Then perlhas to execute the code it created during the interpretation phase.

    So these two keywords have different functions:

    • exit;tells Perl to stop running when it gets here during execution;
    • __END__tells Perl to stop reading the source code during interpretation.

    Thus, your __END__is causing perlto stop reading the source code before it gets to the subroutine; the subroutine is therefore never compiled. During execution, it does not exist, hence the error.

    So, if I'm guessing right about your intentions, the fix is fairly simple, as noted by everyone else on this thread:

    package My::Util; use strict; use warnings; use Exporter; our @ISA = qw(Exporter); our @EXPORT = qw( get_file_name ); exit; sub get_file_name { ... } 1; __END__

      this is sometimes called ... "interpretation" (... more technically correct) ... the interpretation phase

      I don't recall the Perl documentation or the Camel ever referring to it as that... perhaps you meant "parsing"?

      Although the existence of eval and BEGIN really blur the lines, if we're going to be picky about the compilation/runtime phases, then in fact the exit you suggest would be executed during the compilation phase of the main script, causing the rest of the main script after the use to never even be compiled!

      Also, it sounds like you're saying that "everyone else on this thread" suggested exit?

        I don't recall the Perl documentation or the Camel ever referring to it as that... perhaps you meant "parsing"?

        You may translate it that way if you wish. My reference was not Perl-specific. The difference between a compiler and an interpreter is that the compiler converts source code to object code and then its job is done; the interpretter reads the source code and performs the tasks. The exact process used varies by implementation.

        Although the existence of eval and BEGIN really blur the lines

        An interesting observation.

        Also, it sounds like you're saying that "everyone else on this thread" suggested exit?

        I can see where one might draw that inference, but only if they choose to ignore the preponderence of evidence that up to that point in time everyone had, in fact, discussed __END__, not exit. Most reasonable people would not likely jump to the conclusion that exitwas the reference being made.

        Clarification and precision is useful in our profression, to be sure; though the latter is less necessary in general conversation, it pays to be careful. Thank you for the notes.

Re: Subroutines not exporting? Why?
by afoken (Chancellor) on Jul 06, 2015 at 23:36 UTC
    use Exporter; our @ISA = qw(Exporter);

    Just a minor note: Unless your code has to work with perl older than v5.8.3 (from 2004), you don't have to inherit from Exporter to export subroutines. It is sufficient to import Exporter's import method into your module:

    use Exporter qw( import ); # ... and DON'T set @ISA!

    This way, Exporter's other methods, including the internal ones, don't appear as inherited methods of your module.

    See also Re: Advice on style, Re^2: Advice on style.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1133361]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2024-03-28 19:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found