2 examples of packages 1 has error the other does not

B

Billy Patton

I'm writing and testing a module that can be executed as a standalone script
for debugging purposes.
It's part of a very large system of regression testing. Messages are lost
along the way.
So with the caller() function I transform a module into an executable.
With example #1 my $x seems to have lost it's scope.

What I'm worried about, is what is goinf to happen when in the big system it
does a :
use foo;
or
require "foo.pm";

example #1
#!/usr/local/bin/perl
use strict;
use warnings;
if ( !defined caller ) { foo::bar();}
package foo;
my $x = 'foo.bar';
sub bar { print "$x\n"; } <<<<------ line 13 blank lines removed
1;

executing gives this :
Use of uninitialized value in concatenation (.) or string at z line 13.


example #2 works fine
#!/usr/local/bin/perl
package foo;
use strict;
use warnings;
my $x = 'foo.bar';
sub bar { print "$x\n"; }
if ( !defined caller ) {
# enter package main
package main;
use strict;
use warnings;
foo::bar();
} # leave package main
1;
 
M

Mumia W.

I'm writing and testing a module that can be executed as a standalone script
for debugging purposes.
It's part of a very large system of regression testing. Messages are lost
along the way.
So with the caller() function I transform a module into an executable.
With example #1 my $x seems to have lost it's scope.

What I'm worried about, is what is goinf to happen when in the big system it
does a :
use foo;
or
require "foo.pm";

example #1
#!/usr/local/bin/perl
use strict;
use warnings;
if ( !defined caller ) { foo::bar();}
package foo;
my $x = 'foo.bar';
sub bar { print "$x\n"; } <<<<------ line 13 blank lines removed
1;

executing gives this :
Use of uninitialized value in concatenation (.) or string at z line 13.

[...]

Move the line that tests caller() and calls foo::bar() down below the
line that sets $x. Right now, when foo::bar() is invoked, $x has not
been given any value, so it's undefined.
 

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
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top