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

Hi, keepers of Perl knowledge!

I have code where I am constructing the name of a .conf file based on the location of the module:

package Module; use warnings; BEGIN { my $pathname = $INC{$\(__PACKAGE__)}.pm"}; $pathname =~ s/\.pm$/.conf/; } 1;

When I check the syntax via the interpreter, I get a warning:

$ perl -c Module.pm Use of uninitialized value in substitution (s///) at Module.pm line 8. Module.pm syntax OK $

If I comment out the use warnings; pragma, the warning goes away.

I'm assuming that __PACKAGE__ is not defined until a script loads the module, so this might be an explanation. perlmod states that using __PACKAGE__ can be thorny when creating variable names, so I thought I would pose the question to you. Should I be looking at another method to construct a variable name?

Thanks!

Replies are listed 'Best First'.
Re: a few questions about __PACKAGE__
by ikegami (Patriarch) on Feb 10, 2012 at 20:11 UTC

    What you have there does not compile

    syntax error at Module.pm line 5, near "$\(" BEGIN not safe after errors--compilation aborted at Module.pm line 5.

    I presume your code is closer to

    package Module; use strict; use warnings; BEGIN { my $pathname = $INC{"$\(__PACKAGE__)}.pm"}; $pathname =~ s/\.pm$/.conf/; } 1;

    If so, the problem is that

    "$\(__PACKAGE__)}.pm"

    is the same as

    $\ . "(__PACKAGE__)}.pm"

    because $\ is a variable. You want

    my $pathname = $INC{__PACKAGE__ . ".pm"};

    Except that will fail if your module name has more than one part (e.g. "Foo::Bar"), and it's more complicated that it needs to be. You really just want

    my $pathname = __FILE__;
Re: a few questions about __PACKAGE__
by Eliya (Vicar) on Feb 10, 2012 at 20:00 UTC

    I think you want $INC{__PACKAGE__.".pm"};

    Also note that the lexical variable my $pathname would go out of scope at the end of the BEGIN block.

Re: a few questions about __PACKAGE__
by Anonymous Monk on Feb 10, 2012 at 19:58 UTC

    The error message you posted is on line 8, but the code you posted does not have 8 lines

    The code you posted does not compile (typo)

    You're also missing a pair of braces in the interpolation, to dereference the reference you're taking

    The syntax you're looking for, the common idiom, is

    print "@{[ 1,2,'Q', rand ]}\n"; print "@{[ __PACKAGE__ ]}\n"; __END__ 1 2 Q 0.770751953125 main

    references quick reference

      I think the form of that idiom the OP had seen was probably
      $ perl -wE 'print "${\__PACKAGE__}\n";'
      main
      (or "${\(...)}") which is a little less unsightly, if the expression returns a scalar.
Re: a few questions about __PACKAGE__
by JavaFan (Canon) on Feb 12, 2012 at 10:54 UTC
    I'm assuming that __PACKAGE__ is not defined until a script loads the module,
    That's not your problem. The problem is that $INC{"Module.pm"} only gets set after successful complication, and you haven't finished compiling yet.