please splain dis scoping issure

B

bpatton

I have 3 routines seperated by __END__
The BEGIN makes it work
#!/usr/local/bin/
perl
# this works
fine
use strict;
use warnings;
if ( !defined caller ) { foo::foo(); }
package foo;
use strict;
use warnings;
use Data::Dumper; $Data::Dumper::Indent = 1;
use vars qw ( %hash );
BEGIN { %hash = ( a => { b => 1 , c => 2 },); }
sub foo { print Dumper(\%hash); }
__END__
#!/usr/local/bin/
perl
#This produces an empty
hash
use strict;
use warnings;
if ( !defined caller ) { foo::foo(); }
package foo;
use strict;
use warnings;
use Data::Dumper; $Data::Dumper::Indent = 1;
use vars qw ( %hash );
%hash = ( a => { b => 1 , c => 2 },);
sub foo { print Dumper(\%hash); }
__END__
#!/usr/local/bin/
perl
#This dumps an empty hash
table
use strict;
use warnings;
if ( !defined caller ) { foo::foo(); }
package foo;
use strict;
use warnings;
use Data::Dumper; $Data::Dumper::Indent = 1;
my %hash = ( a => { b => 1 , c => 2 },);
sub foo { print Dumper(\%hash); }
 
B

Brian McCauley

Subject: please splain dis scoping issure

Many of the regulars here speak English as a second language. Please
don't write using phonetic representation of a a particular English
accent - it will be very confusing to them.

BTW this is not a scoping issue. It's an execution order issue.
I have 3 routines seperated by __END__

A blank line too would be good!
The BEGIN makes it work
#!/usr/local/bin/
perl
# this works
fine


I think
that you
need a
wider screen
:)

use strict;
use warnings;
if ( !defined caller ) { foo::foo(); }
package foo;
use strict;
use warnings;
use Data::Dumper; $Data::Dumper::Indent = 1;
use vars qw ( %hash );
BEGIN { %hash = ( a => { b => 1 , c => 2 },); }
sub foo { print Dumper(\%hash); }
__END__
#!/usr/local/bin/
perl
#This produces an empty
hash
use strict;
use warnings;
if ( !defined caller ) { foo::foo(); }
package foo;
use strict;
use warnings;
use Data::Dumper; $Data::Dumper::Indent = 1;
use vars qw ( %hash );
%hash = ( a => { b => 1 , c => 2 },);
sub foo { print Dumper(\%hash); }
__END__

That's right, you call foo::foo() before you put anything in %hash.

You should probably move the line...

if ( !defined caller ) { foo::foo(); }

....to just before __END__

BTW: It's more simply written

foo unless caller;
#!/usr/local/bin/
perl
#This dumps an empty hash
table
use strict;
use warnings;
if ( !defined caller ) { foo::foo(); }
package foo;
use strict;
use warnings;
use Data::Dumper; $Data::Dumper::Indent = 1;
my %hash = ( a => { b => 1 , c => 2 },);
sub foo { print Dumper(\%hash); }

For the same reason as the second one.
 
J

Jürgen Exner

bpatton wrote:

Subject: please splain dis scoping issure
Spell checker suggested: please slain dies scoping issuer

I have no idea what you mean by that.

jue
 

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

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top