Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Question about the order of declaring things in perl

by mkalas (Initiate)
on Oct 18, 2013 at 02:45 UTC ( #1058692=perlquestion: print w/replies, xml ) Need Help??
mkalas has asked for the wisdom of the Perl Monks concerning the following question:

I have some code that needs maintaining and rewriting and I am truly a newbie. In the first part of the script I see
my ($Dev,$Inode) = stat("$LOG_DIRECTORY\/$FILE"); $LOG_FILE_HASH{$FILE}->{Current_Dev} = $Dev; $LOG_FILE_HASH{$FILE}->{Current_Inode} = $Inode;
This looks like a dereference for a hash value. But the hash %LOG_FILE_HASH does not appear until later in the code.

With the initial syntax is there an implicit creation of the hash LOG_FILE_HASH, or does it not matter so long as the hash is created at some point in the code. There are no includes, or packages. Just subroutines called in order. When parsing the code the actual hash %LOG_FILE_HASH is in an isolated subroutine about 30 lines later.

Replies are listed 'Best First'.
Re: Question about the order of declaring things in perl
by kcott (Chancellor) on Oct 18, 2013 at 04:18 UTC

    G'day mkalas,

    Welcome to the monastery.

    While the order of declarations may be a factor, the scope of the declarations will typically be more important. Furthermore, the order in which code appears may not necessarily be the same order in which it is compiled.

    You'll need to show the code where %LOG_FILE_HASH appears. It may be scoped to the subroutine: in this case, it will be a completely different %LOG_FILE_HASH to the one referenced by $LOG_FILE_HASH{$FILE}; alternatively, it may be exactly the same variable.

    There's far too many possibilities to try and guess what you might have and then to describe each one: post your code (in <code>...</code> tags, as LanX has already pointed out) and a definitive answer can be provided.

    Here's some general reading that mostly covers this topic: sub; perlsub; use; perlmod: BEGIN, UNITCHECK, CHECK, INIT and END. I expect that will be rather heavy going: with a better idea of your code, a simplified explanation can be provided.

    -- Ken

Re: Question about the order of declaring things in perl
by LanX (Chancellor) on Oct 18, 2013 at 03:03 UTC
    Welcome to the monastery! :)

    Plz Posts are HTML formatted!

    Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!

    I suppose you are not using strict, otherwise you would be forced to declare %LOG_FILE_HASH before using it.

    Regarding the nesting, Perl has auto-vivification, so the data-structure will spring into life.

    DB<100> $h{bar}{foo}=42 => 42 DB<101> \%h => { bar => { foo => 42 } }

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1058692]
Approved by kevbot
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2017-07-23 04:21 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (343 votes). Check out past polls.