Ben Siders wrote:
> 
> Ben Siders wrote:
> >
> > Ok, this is the strangest thing.  I've been staring at it for an hour,
> > and it has to be something monumentally stupid.
> >
> > I have a function that is given a hash reference and it then just
> > looks up one of its elements in an anonymous hash that points to other
> > functions.  Here's the function (I removed the part where it calls
> > another function - this is just demonstrating the strange behavior):
> >
> > my $db_table = {
> >    codeline => \&write_codeline,
> >    compounddef => \&write_compound,
> >    includes => \&write_includerelation,
> >    initializer => \&write_initializer,
> >    innerclass => \&write_innerclass,
> >    param => \&write_param,
> >    type => \&write_type,
> > };
> >
> > sub writeToDatabase
> > {
> >    my $this = shift;
> >    print "[$this->{hashRef}->{tag}] \n";
> >    return unless ( exists $db_table->{$this->{hashRef}->{tag}} );
> >    print "We didn't return.\n";
> > }
> >
> > Pretty straightforward?  Here's a sample run of the program:
> >
> > [location]
> > [includes]
> > [compounddef]
> > [includes]
> > [includes]
> > [includes]
> > [includes]
> > [includes]
> > [includes]
> >
> > Notice that 'includes' and 'compounddef' are in that $db_table
> > reference?  And they're clearly in the $this hash.  So why isn't it
> > finding them in my $db_table hash?
> 
> OK apparantly I have a scoping problem below.  If I add this line:
> 
> print "ok\n" if exists $db_table->{codeline};
> 
> inside my function, I get nothing.  If I put it right after the db_table
> declaration, it prints.  So... are 'my' variables outside of a function
> not visible inside it?

Lexical scope flows from the declaration to the end of the block or file
so if $db_table is declared below sub writeToDatabase then the sub won't
see it.



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to