Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

declare/initialize variables at bottom of script?

by bcarroll (Monk)
on Jun 11, 2013 at 17:52 UTC ( #1038298=perlquestion: print w/ replies, xml ) Need Help??
bcarroll has asked for the wisdom of the Perl Monks concerning the following question:

I always use strict, and usually declare/initialize variables at the top of a script.

use warnings; use strict; my ($str1, $str2, $str3); my @arr = ('1', '2', '3'); ... do stuff with the variables ...

Does perl have any magical tricks to put the variable declaration/initialization in a subroutine or some other mechanism so that is not the top chunk of the script?
something like this (which does not work)

use warnings; use strict; initializeVars(); ... do stuff with the variables ... sub initializeVars{ our ($str1, $str2, $str3); our @arr = ('1', '2', '3'); }

Comment on declare/initialize variables at bottom of script?
Select or Download Code
Re: declare/initialize variables at bottom of script?
by 5mi11er (Deacon) on Jun 11, 2013 at 18:06 UTC
    Well, you could wrap all your code in subroutines at the top, then do the initializations and call some "main()" routine at the bottom, kinda like C code...

    Otherwise, I am unaware of any magic options.

    -Scott

      I think I used to do that years ago... not sure why I stopped...

      Thanks!

Re: declare/initialize variables at bottom of script?
by BrowserUk (Pope) on Jun 11, 2013 at 18:18 UTC
    usually declare/initialize variables at the top of a script.

    I much prefer to declare (and if required; initialise) variables at their point of first use. I can see no advantage of your practice?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: declare/initialize variables at bottom of script?
by Preceptor (Chaplain) on Jun 11, 2013 at 18:20 UTC
    Initializing you could do somewhere else, but declaration must happen before you attempt to use them. Either via 'my' declaration at the top of your script, or you could perhaps import them from a module. In neither case can you declare variables at the 'bottom' but you could do so at the top of a short module as a separate piece of code.
Re: declare/initialize variables at bottom of script?
by dcmertens (Beadle) on Jun 11, 2013 at 21:31 UTC
    If you're willing to foregoe strictness (not recommended), you could use globals and a BEGIN block:
    no strict; no warnings; some_operation_on($abc); ... BEGIN { $abc = 'foo'; }
    If you want to keep strictness, you have to declare them before their first use. Again, you could employ the BEGIN bock trick to assign their initial values at the end of the script.
Re: declare/initialize variables at bottom of script?
by Laurent_R (Parson) on Jun 11, 2013 at 22:11 UTC

    I ... usually declare/initialize variables at the top of a script.

    Don't do that. It is a bad idea.

    I was also doing that at my beginning in Perl, because of my C background (I think it has changed in C, but there was a time where all variables had to be declared either at the beginning of each function, or at the beginning of the program for global variables.

    Declaring all your variables at the beginning of the program defeats at least a good half of the advantages of lexical variables. It makes your variable essentially global, which is precisely what you don't want to do. You should rather declare your variables in the smallest possible lexical scope (function level, loop level, block level, whatever).

Re: declare/initialize variables at bottom of script?
by AnomalousMonk (Abbot) on Jun 11, 2013 at 22:13 UTC

    If you want to go old-school, there's also the vars pragma for declaring package variables (which is what you get with our):
        use vars qw($scalar @array %hash $etc);

    But I must say I second BrowserUk's preferred practice and would echo his question: what advantage do you see for doing it your way?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1038298]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2014-10-24 08:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (131 votes), past polls