Namespace is changed before "package" line is reached

Namespace is changed before "package" line is reached

am 30.11.2007 19:49:47 von David Worenklein

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;

<<>>

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

package test;


1;


<<>>

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

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

am 30.11.2007 21:09:38 von Claudio Calvelli

On 2007-11-30, David Worenklein 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.

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

am 30.11.2007 22:24:44 von Big and Blue

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.

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

am 30.11.2007 22:36:06 von Big and Blue

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.

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

am 30.11.2007 23:34:06 von Claudio Calvelli

On 2007-11-30, Big and Blue 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.

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

am 01.12.2007 04:31:41 von Big and Blue

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.