Namespace is changed before "package" line is reached

Try this. test.pl is being loaded before the "package" line, yet it
is evaluating within the package. Why would that be? Thanks.

use test;

<<<test.pm>>>

print __FILE__, ":", __LINE__, " - package is ", __PACKAGE__, "\n";
require "test2.pl";
print __FILE__, ":", __LINE__, " - package is ", __PACKAGE__, "\n";

package test;


1;


<<<test2.pl>>>

print __FILE__, ":", __LINE__, " - package is ", __PACKAGE__, "\n";
1;



Here's what I get:

test.pm:1 - package is main
test2.pl:1 - package is test <<==== Expected this to say main
test.pm:3 - package is main
David Worenklein [ Fr, 30 November 2007 19:49 ] [ ID #1883194 ]

Re: Namespace is changed before "package" line is reached

On 2007-11-30, David Worenklein <dcw [at] gcm.com> wrote:
[...]
> Here's what I get:
>
> test.pm:1 - package is main
> test2.pl:1 - package is test <<==== Expected this to say main
> test.pm:3 - package is main

What version of perl are you using?

perl -e 'use test'
test.pm:1 - package is main
test2.pl:1 - package is main
test.pm:3 - package is main

perl -v

This is perl, v5.8.8 built for x86_64-linux-thread-multi-ld

Copyright 1987-2006, Larry Wall
[...]

C

--
The address in the "From" header won't work. Email to "usenet" at "intercal" dot
"dyn-o-saur" dot "com" may or may not reach me, depending on how far it manages
to go through the spam filter, and other conditions which I won't disclose.
Claudio Calvelli [ Fr, 30 November 2007 21:09 ] [ ID #1883195 ]

Re: Namespace is changed before "package" line is reached

David Worenklein wrote:

> Try this. test.pl is being loaded before the "package" line, yet it
> is evaluating within the package. Why would that be? Thanks.

If you run test.pm as a module you get what you say.

[my-sys]: perl -Mtest
test.pm:1 - package is main
test2.pl:1 - package is test
test.pm:3 - package is main

but if you run test.pm as a script you get what Claudio Calvelli saw:

[my-sys]: perl test.pm
test.pm:1 - package is main
test2.pl:1 - package is main
test.pm:3 - package is main

Presumably you did the former - your description wasn't clear.

--
Just because I've written it doesn't mean that
either you or I have to believe it.
Big and Blue [ Fr, 30 November 2007 22:24 ] [ ID #1883196 ]

Re: Namespace is changed before "package" line is reached

Big and Blue wrote:
>
> Presumably you did the former - your description wasn't clear.

So the reason will be down to differences between what is run when
(compile time or run time) when you run teh code as a module or as a program
script.

A few BEGIN{} and INIT{} blocks might help to figure it out.


--
Just because I've written it doesn't mean that
either you or I have to believe it.
Big and Blue [ Fr, 30 November 2007 22:36 ] [ ID #1883197 ]

Re: Namespace is changed before "package" line is reached

On 2007-11-30, Big and Blue <No_4 [at] dsl.pipex.com> wrote:
> [my-sys]: perl -Mtest
> test.pm:1 - package is main
> test2.pl:1 - package is test
> test.pm:3 - package is main
>
> [my-sys]: perl test.pm
> test.pm:1 - package is main
> test2.pl:1 - package is main
> test.pm:3 - package is main

Hmmm, I see "main" in both cases:

perl -Mtest
test.pm:1 - package is main
test2.pl:1 - package is main
test.pm:3 - package is main

perl test.pm
test.pm:1 - package is main
test2.pl:1 - package is main
test.pm:3 - package is main

Same result if I try with 5.8.7 or 5.8.8

C

--
The address in the "From" header won't work. Email to "usenet" at "intercal" dot
"dyn-o-saur" dot "com" may or may not reach me, depending on how far it manages
to go through the spam filter, and other conditions which I won't disclose.
Claudio Calvelli [ Fr, 30 November 2007 23:34 ] [ ID #1883198 ]

Re: Namespace is changed before "package" line is reached

Claudio Calvelli wrote:

> Hmmm, I see "main" in both cases:

Good point.

I also get main all the time with 5.8.8.

I was actually using 5.6.1 for the check I did, so the module case is
version-dependent.


--
Just because I've written it doesn't mean that
either you or I have to believe it.
Big and Blue [ Sa, 01 Dezember 2007 04:31 ] [ ID #1883761 ]
Perl » comp.lang.perl.modules » Namespace is changed before "package" line is reached

Vorheriges Thema: What can you use to tie a hash of hashes to either dbm or ascii?
Nächstes Thema: To publish on CPAN or not? And architectural q. on modules.