2

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?

Brad Gilbert
  • 33,846
  • 11
  • 78
  • 129
algo-geeks
  • 5,280
  • 10
  • 42
  • 54

4 Answers4

4

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;
Alex Howansky
  • 50,515
  • 8
  • 78
  • 98
3

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.

Community
  • 1
  • 1
Andy
  • 4,789
  • 23
  • 20
2

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
    
Brad Gilbert
  • 33,846
  • 11
  • 78
  • 129
  • 2
    That does not temporarily set the value of a variable to something else. `local` does that, though. – tchrist Mar 19 '11 at 22:41
  • 1
    @tchrist, Brad's code works for temporarily setting the value of a lexical variable. Though if you are using package variables and you want the change to be visible in the dynamic scope, `local` is the way. – dsolimano Mar 19 '11 at 23:12
  • 3
    @dsolimano: No, it is not temporarily setting the value of a lexical variable. It is a different variable altogether, one which always has that same value. – tchrist Mar 19 '11 at 23:39
  • The third example is still misworded. Consider for example `{ my $saluations = $salutations . "buddy"; ⋯ }`, which proves they are completely separate variables. So too will taking their address, since `\$saluations` will give a different number inside the block than it gives outside the block, even when their values are the same. Don’t confuse variables with values, please. That block creates a completely brand-new variable that happens to have the same name as a variable from an enclosing lexical scope. – tchrist Mar 20 '11 at 14:56
-2

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

BadFileMagic
  • 701
  • 3
  • 7
  • 2
    Autoviv has nothing to do with variable declaration or lack thereof. – tchrist Mar 19 '11 at 22:39
  • @tchrist i've seen code where hash is not pre-declared, and elements are autovived in a loop based on magic variables. i know misused autoviv to mean pulling a var out off thin air without a predeclaration, but my comment against black magic still stands. – BadFileMagic Mar 19 '11 at 23:13
  • This is just FUD. There is no "magically appearing" thing that has anything to do with whether a hash is declared or not. Neither is there anything involved with "magic variables", whatever the devil that means. **−①** – tchrist Mar 19 '11 at 23:42
  • if you show us the code you are referring to, then we can explain it to you. If you don't, then we can't. – tadmc Mar 20 '11 at 05:30
  • @tadmc, I don't need help understanding it, I just didn't really appreciate it when I first came into the project and had to contort my brain out of its C-style declare-before-use mindset. – BadFileMagic Mar 20 '11 at 16:34