BEGIN { package Foo; use Foo }

K

kj

One of the things I find most infuriating about Config::Std (aside
from the fact that there's nothing standard about it) is that it
imports the subroutine read_config into the invoking namespace,
whether it is requested or not. I.e. even after

package MyPackage;
use Config::Std ();

one still ends up with a MyPackage::read_config subroutine that
one explicitly did not want.

And one can't simply revert to

BEGIN { require Config::Std; }

because, as it turns out, there is no Config::Std::read_config().
Therefore, if one forgoes the services of Config::Std::import, one
must then go behind the API, because the subroutine that normally
would get exported as "read_config" is actually called
Config::Std::Hash::read_config, at least this time around.

(BTW, these shenanigans seem to me *egregious* coming from the
author of "Perl Best Practices." If these are best practices in
the Perl world, I don't want to see what passes for so-so practices.)

I am sorely tempted to do something like

BEGIN { package Config::Std; use Config::Std }

but in the end I don't know who's more likely to be hoist by his
own petard.

Is there a better work-around to prevent Config::Std from defecating
all over my namespace (other than avoiding Config::Std altogether)?

Bah-humbugly yours,

kj
 
E

Eric J. Roode

One of the things I find most infuriating about Config::Std (aside
from the fact that there's nothing standard about it) is that it
imports the subroutine read_config into the invoking namespace,
whether it is requested or not. I.e. even after

package MyPackage;
use Config::Std ();

one still ends up with a MyPackage::read_config subroutine that
one explicitly did not want.

That is not true. If you include an empty list after the package name,
the module's import() subroutine isn't even called. Did you try this?

[...]
Is there a better work-around to prevent Config::Std from defecating
all over my namespace (other than avoiding Config::Std altogether)?

Config::Std exports a grand total of two (count'em, two!) symbols into
your namespace. Did you really need to use read_config and write_config
yourself?

If this truly is a problem, I would suggest inventing a namespace for
holding the Config::Std symbols.

package HoldingPen;
use Config::Std;
package MyPackage;
....
HoldingPen::read_config 'file.cfg' => my %config;

--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 
K

kj

In said:
@reader1.panix.com:
That is not true. If you include an empty list after the package name,
the module's import() subroutine isn't even called. Did you try this?

Of course I did. And I also read the source code of Config::Std.
Did you? Do you realize that what you describe, though it does
apply to modules that inherit from Exporter, is not true in general?
One can make import do whatever one wants.

kj
 
E

Eric J. Roode

In <[email protected]> "Eric J. Roode"



Of course I did. And I also read the source code of Config::Std.
Did you?

I not only read the Config::Std source code before posting, I also wrote
two short test programs.

perl -e 'package Foo; use Config::Std; print "defined\n"
if defined &Foo::read_config'

perl -e 'package Foo; use Config::Std (); print "defined\n"
if defined &Foo::read_config'

The first prints "defined". The second does not.
Do you realize that what you describe, though it does
apply to modules that inherit from Exporter, is not true in general?
One can make import do whatever one wants.

One thing one cannot make import do: make things happen when it isn't
even called.

Try this:

# (in file Foo.pm)
package Foo;
sub import
{
print STDERR "Wahoo!!\n";
}

then:
perl -e 'use Foo'
then:
perl -e 'use Foo ()'

--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 
P

Paul Lalli

kj said:
Of course I did. And I also read the source code of Config::Std.
Did you? Do you realize that what you describe, though it does
apply to modules that inherit from Exporter, is not true in general?
One can make import do whatever one wants.

Uh. use MyPackage; vs use MyPackage(); has nothing to do with "making
import do whatever one wants", and certainly nothing to do with
Exporter. It's the `use` call that differs.

Please read:
perldoc -f use

Paul Lalli
 
K

kj

I not only read the Config::Std source code before posting, I also wrote
two short test programs.
perl -e 'package Foo; use Config::Std; print "defined\n"
if defined &Foo::read_config'
perl -e 'package Foo; use Config::Std (); print "defined\n"
if defined &Foo::read_config'
The first prints "defined". The second does not.

I stand corrected. My apologies.

The fact remains, however, that one cannot refer to
"Config::Std::read_config" because it doesn't exist. One must
either accept the importation of read_config into some namespace
or else go behind the API and refer to Config::Std::Hash::read_config.

The workaround I cited is

BEGIN { package Config::Std; use Config::Std }

but I think it has a lot of problems. My original question then
remains: what's the best way to avoid having read_config imported
and still know how to refer to it?

Another possibility is

BEGIN { package Config::Std::Some::Crazy::Name; use Config::Std }

Then the desired function is reliably
Config::Std::Some::Crazy::Name::read_config, though I have to hope
that I have not inadvertently introduced a collision.

kj
 
E

Eric J. Roode

The workaround I cited is

BEGIN { package Config::Std; use Config::Std }

but I think it has a lot of problems.

I agree; I'm not sure I'd trust this not to break something.
My original question then
remains: what's the best way to avoid having read_config imported
and still know how to refer to it?

Why is importing such an issue?
Another possibility is

BEGIN { package Config::Std::Some::Crazy::Name; use Config::Std }

Then the desired function is reliably
Config::Std::Some::Crazy::Name::read_config, though I have to hope
that I have not inadvertently introduced a collision.

That's about the only thing I can think of....

--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top