[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Location of MAIN code relative to subroutines



In the code you posted, the use of the variables $inputfile and 
$TIME_OFFSET *within* the subroutine definitions refer to the 
*package* (or "global," if you will) variables $main::inputfile 
and $main::TIME_OFFSET.  As such, any changes you make to one 
of the package variables in the subroutine will be seen by 
the other.

However, in the main part of your routine, you also define two 
*lexically-scoped* variables $TIME_OFFSET and $inputFile, which, 
as Paul noted, have nothing to do with the package variables and 
only exist within that lexical scope.

In the case of $TIME_OFFSET, you assigned a value to the lexically-
scoped variable and tried to read from the package value and 
failed to get anything.  In the case of $inputFile, however, 
all interaction is within subroutines and hence involving the 
package variable.  The lexical variable "$inputFile", though 
defined, is never used within its lexical scope.

If, however, the "my" statements are placed before the 
subroutine blocks, then those blocks are within the lexical 
scope of the my statements, and the routines will use 
the lexical variables and not package variables.

A recent article in TPJ goes into a fair amount of detail on the 
differences between lexical and package variables.

HTH,

-C

At 1:05 PM -0600 4/27/1999, Ero Brown wrote:
>Thanks for your reply Paul.
>
>So how does the $inputfile variable escape the same fate
>as the $TIME_OFFSET variable?  Given the definition of "my"
>variables you provided from perlsub, wouldn't the value of
>the $inputfile in the "ParseFile" sub be in question as well?
>
>Ero Brown
>
>-------------------------------------------------------------
>
>
>>On Tue, Apr 27, 1999 at 11:41:32AM -0600, Ero Brown wrote:
>>}
>>} I seem to ben having a problem regarding the placement of "MAIN"
>>} code relative to subroutines.
>>}
>>} When I execute the following script, the variable $TIME_OFFSET
>>} doesn't seem to have value in the "ParseFile" subroutine.
>>}
>>} However, if I move the location of my "MAIN" code (or actually
>>} just the code snip in "MAIN" where $TIME_OFFSET is defined) to
>>} a position before my subroutines, this script appears to work
>>} correctly.
>>}
>>} Is this a MacPerl bug, or am I missing something here?
>>} I'm currently using MacPerl 5.2.0r4.
>>
>>You're missing something.  From perlsub (I don't remember what Shuck
>>calls it):
>>
>>
>>     Unlike dynamic variables created by the "local" operator,
>>     lexical variables declared with "my" are totally hidden from
>>     the outside world, including any called subroutines (even if
>>     it's the same subroutine called from itself or elsewhere--
>>     every call gets its own copy).
>>
>>$TIME_OFFSET only exists from the point you declare it to the end of
>>the file.  The subroutines never see it.
>>
>>
>>}
>>} Thanks,
>>}
>>} Ero Brown
>>}
>>
>>--
>>Paul Schinder
>>schinder@pobox.com
>
>
>
>
>===== Want to unsubscribe from this list?
>===== Send mail with body "unsubscribe" to macperl-request@macperl.org




===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org