PedroA
PedroA

Reputation: 1925

Correct way of variable declaration in Perl

I have a set of 3 or 4 separate Perl scripts that used to be part of a simple pipeline, but I am now trying to combine them in a single script for easier use (for now without subroutine functions). The thing is that several variables with the same name are defined in the different scripts. The workaround I found was to give different names to those variables, but it can start to become messy and probably it is not the correct way of doing so. I know the concept of global and local variables but I do not quite understand how do they exactly work.

Are there any rules of thumb for dealing with this sort of variables? Do you know any good documentation that can shed some light on variable-scope or have any advise on this?

Thanks.

EDITED: I already use "use warnings; use strict;" and declare variables with "my". The question might actually be more related to the definition of scoping blocks and how to get them to be independent from each other...

Upvotes: 2

Views: 386

Answers (2)

FMc
FMc

Reputation: 42411

The rule of thumb is to put your code in subroutines, each of them focused on a simple, well-defined part of the larger process. From this one decision flow many virtuous outcomes, including a natural solution to the variable scoping problem you asked about.

sub foo {
    my $x = 99;
    ...
}

sub bar {
    my $x = 1234;  # Won't interfere with foo's $x.
    ...
}

If, for some reason, you really don't want to do that, you can wrap each section of the code in scoping blocks, and make sure you declare all variables with my (you should be doing the latter nearly always as a matter of common practice):

{
    my $x = 99;
    ...
}

{
    my $x = 1234;  # Won't interfere with the other $x.
    ...
}

Upvotes: 5

DavidRR
DavidRR

Reputation: 19397

You are likely getting into trouble because of your use of global variables (which actually likely exist in package main). You should try to avoid the use of global variables.

And to do so, you should become familiar with the meaning of variable scope. Although somewhat dated, Coping with Scoping offers a good introduction to this topic. Also see this answer and the others to the question How to properly use Global variables in perl. (Short Answer: avoid them to the degree possible.)

The principle of variable scope and limiting use of global variables actually applies to nearly all programming languages. You should get in the habit of declaring variables as close as possible to the point where you are actually using them.

And finally, to save yourself from a lot of headaches, get in the habit of:

  • including use strict; and use warnings; at the top every Perl source file, and
  • declaring variables with my within each of your sub's (to limit the scope of those variables to the sub).

(See this PerlMonks article for more on this recommendation.)

I refer to this practice as "Perl programming with your seat belt on." :-)

Upvotes: 5

Related Questions