algo-geeks
algo-geeks

Reputation: 5448

relevance of declaring variable in perl

In Perl programming, what is the relevance of variable declaration, when we can change value contained by it?

e.g.

my $name ="johny";

In the next statement we can change type of value contained by it with:

$name = 10;

Then what is relevance of declaring variable, and how does Perl allcate memory to variables when it's type is not fixed?

Upvotes: 2

Views: 361

Answers (4)

Brad Gilbert
Brad Gilbert

Reputation: 34130

There are several reasons to declare a variable.

  • To declare the scope of the variable.

    {
      my $salutations = "hello\n";
    }
    print $salutations; # undef
    
  • To catch misspellings when used with use strict;.

    use strict;
    my $salutations = "hello\n";
    print $_salutations;
    # Global symbol "$_salutations" requires explicit package name ...
    

    Which is why it is recommended to place use strict; at the beginning of your programs.

  • To reuse a variable name, in a new block, without affecting the enclosing block.

    my $salutations = "hello\n";
    {
      my $salutations = "hi there\n";
      print $salutations;
      # prints: hi there
    }
    print $salutations;
    # prints: hello
    

Upvotes: 2

Andy
Andy

Reputation: 4866

You are right that variable declarations aren't needed to distinguish integer/float/... types like in C.

I think of perl variable declarations not as memory allocation instructions, more as namespace instructions specifying when $name is valid. Memory will be allocated/freed as needed by the interpreter. Building a really long string in $name will cause more memory to be allocated for it.

Declaring a variable with "my $var" makes it local to the current scope. Without the "my" declaration, it is a global variable. Usually subroutines should declare any variables they use with "my" to avoid polluting the global namespace. For more background read the perlsub documentation.

Where the variable is declared can have performance implications since it may cause the variable to be destroyed/re-created, as discussed here.

Upvotes: 3

BadFileMagic
BadFileMagic

Reputation: 701

The use of special variables and gratuitous auto-vivification leads to a reliance on black magic. also, it affects scoping of variables which makes keeping track of program flow harder and thus maintenance and debugging are harder.

To the other part of your question, Perl data types are scalar, array and hash. Unlike lower-level languages, or some other more strongly-typed languages, primitive data types don't really come into play. As to how it's actually handled internally in the compiler/interpreter, I'm not 100% sure, but a good place to start might be here: http://dev.perl.org/perl5/docs/perlhack.html

Upvotes: -2

Alex Howansky
Alex Howansky

Reputation: 53646

On of the advantages of declaring variables is to help reduce bugs, especially when combined with:

use strict;
use warnings;

In this case, if you mistype a variable name, Perl will tell you that you're referencing an undeclared variable:

my $name = 'johnny';
print $nane;

Upvotes: 4

Related Questions