----------  Forwarded Message  ----------
Subject: Re: Declare $main::scalar in begin with 'use strict'
Date: Thu, 21 Nov 2002 16:51:43 -0600
From: Dr. Poo <[EMAIL PROTECTED]>
To: [EMAIL PROTECTED]


Dude, you rule. That's all i have to say besides, yah, you rule.

This worked perfectly. Why the hell havn't i ever seen INIT before?

Oh well, i have now. Thanks again.

One more thing though. In my code, i *HAD* BEGIN and END preceded by the sub
keyword...Would that have any ill effect to the program?
had this --
        sub BEGIN
        {
        ...
        ...
        }

        sub END
        {
        ...
        ...
        }

In your code you have something like this (the {}'s are different)
        INIT
        {
        ...
        ...
        }

So i then decided, hmm... i guess i don't need them on any of the
"pre-processor" directive. And now i have this,

        BEGIN
        {
        ...
        ...
        }

        END
        {
        ...
        ...
        }

Any reason why to do one over the other?

Again, thanks a million. And i hope this helps other's as much as it did me.

On Tuesday 19 November 2002 08:43 pm, you wrote:
> On Nov 19, Dr. Poo said:
> >I'm trying to declare a variable $log that is a reference to an array that
> >will contain the small amounts of data that need to be logged throughout a
> >backup script i'm working on. I'd like to declare this scalar reference in
> >the BEGIN function, but i can't use it outside of the BEGIN function if i
> >declare 'my $log' in BEGIN.
>
> A my() declaration happens at compile-time.  The assignment to the
> variable happens at run-time.  That means when you write
>
>   my $x = foo();
>
> Perl is really doing
>
>   my $x;
>
> at compile-time, and
>
>   $x = foo();
>
> at run-time.  So your code would be somthing like:
>
>   my $log;
>   BEGIN { $log = ... }
>
> The other problem is calling a function.  You can't call a function in a
> BEGIN block unless it has already been defined.  This is because stuff in
> a BEGIN block is executed at compile-time, as SOON as it is reached.  That
> means that code like:
>
>   BEGIN { foo() }
>   sub foo { ... }
>
> fails because when you try calling foo() at compile-time, Perl hasn't seen
> the definition of the function yet.  Thus, you must do the BEGIN block
> after the definition of the function.
>
>   my $log;
>   sub set_log { ... }
>   BEGIN { $log = set_log() }
>
> But that can look ugly if set_log() is big.  You might be tempted to put
> the function definition and BEGIN block at the END of your code:
>
>   my $log;
>   ...
>   ...
>   sub set_log { ... }
>   BEGIN { $log = set_log() }
>
> but then you have code at the BOTTOM of your program that happens "first".
>
> If you're using Perl 5.6, I have the solution for you.  You can use the
> INIT block.  It is executed immediately after compile-time.
>
>   my $log;
>   INIT { $log = set_log() }
>   ...
>   sub set_log { ... }
>
> Try it out.

-------------------------------------------------------

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

Reply via email to