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

I have a number of scripts that load a particular module, using use in the usual way. In creating a new script that access the module in the same way,
use lib path use module
fails with a syntax error in the module. On the other hand,
require path/module.pm
executes as expected. using
BEGIN ...
does not effect either result. I would appreciate suggestions on what might be going on, or guidance on how to discover what Perl might be objecting to in the module, which (to repeat myself) compiles without error.

Replies are listed 'Best First'.
Re: Curious behavior of Use
by haj (Vicar) on Feb 20, 2025 at 18:53 UTC

    Your code is missing a semicolon after use lib path.

    My glass orb refuses to make further assumptions :)

Re: Curious behavior of Use
by etj (Priest) on Feb 20, 2025 at 18:34 UTC
    I think a full SSCCE would help here. Also, ensuring the environments of both runs are the same, and optionally making each script print its @INC would help. So would sharing the output of perl -V.
        use Switch;

        Well, that's a likely problem right there. Source filters tend to introduce this sort of trouble. From the FAQ:

        Starting from Perl 5.8, a source filter module, Switch, can also be used to get switch and case. Its use is now discouraged, because it's not fully compatible with the native switch of Perl 5.10, and because, as it's implemented as a source filter, it doesn't always work as intended when complex syntax is involved.

        🦛

        If you look at that, you'll see it's unreadable. You need to put <c> tags around it. Also, why not just post it on this thread, instead of your scratchpad? It's OK to sanitise it.

        That's hardly a minimal demonstration.

        And I can't replicate it. I get the following even if I comment out the require:

        Use of uninitialized value $me in string eq at t.pl line 19. Use of uninitialized value $me in string eq at t.pl line 26. Use of uninitialized value $me in string eq at t.pl line 33.
Re: Curious behavior of Use
by ikegami (Patriarch) on Feb 21, 2025 at 01:35 UTC

    Something you said isn't true. (And I'm not referring to the fact that neither of your snippets are Perl code.) Please provide code you actually ran.

Re: Curious behavior of Use
by cavac (Prior) on Feb 24, 2025 at 16:04 UTC

    Say your module is in /home/geoffleach/src/lib/My/Example.pm, something like this should work:

    # from memory, not tested before posting ... BEGIN { unshift @INC, "/home/geoffleach/src/lib"; }; use My::Example; ...

    It's important that the BEGIN block is before the use statement.

    PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP
    Also check out my sisters artwork and my weekly webcomics

      That's an incorrect way of writing the following:

      use lib "/home/geoffleach/src/lib"; use My::Example;

      The OP claimed this doesn't work. Your version won't work any better. (It's in fact worse since it doesn't include the related arch dir.)

      (Also, it doesn't make much sense to hardcode absolute paths to library directories. Use PERL5LIB for absolute paths, and use use lib for relative paths.)