1

I'm new to Perl programming. I've noticed that every time I want to declare a new variable, I should use the my keyword before that variable if strict and warnings are on (which I was told to do, for reasons also I do not know.)

So how to declare a variable in perl without using my and without getting warnings?

My question is: Is it possible to declare a variable without using my and without omitting the use strict; and use warnings; and without getting warnings at all?

serenesat
  • 4,611
  • 10
  • 37
  • 53
ruggedbuteducated
  • 1,318
  • 7
  • 17
  • 22
  • you can get rid of `use strict` and `-w` flag to stop using `my`...BUT THIS IS NOT RECOMMENDED...using `my` will save you a lot of debugging time in future :) – Bill May 22 '13 at 01:36
  • Your question is very confusing. I *think* the problem is that you don't know what it means to "declare" a variable, and are misusing the term. You might want to read through [the Wikipedia article on "Declaration (computer programming)"](http://en.wikipedia.org/wiki/Declaration_%28computer_programming%29). – ruakh May 22 '13 at 01:37
  • 2
    Why don't you want to use `my`? – Keith Thompson May 22 '13 at 01:40
  • i diddnt say i dont want to, i'm just asking if it's possible – ruggedbuteducated May 22 '13 at 01:41
  • 2
    Yes: `use vars qw( $varname );`, or `our $varname;`, or subvert strict checking by using fully qualified names such as `$main::varname`. But these are not synonyms for `my $varname;`. They declare package globals rather than pad lexicals, and have varying scoping rules. – DavidO May 22 '13 at 03:10

3 Answers3

9

There are two means of declaring lexical variables: my and our, but they're not interchangeable, as the lexical created by our is aliased to a package variable.

The vars pragma and our can be seen as means of declaring package variables.

use strict; wont die for declared variables, and use warnings; wont warn for declared variables (unless you count "used only once"), so I have no idea why you mentioned them.

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • i have encountered that term many times - 'packaged variable' what is it? – ruggedbuteducated May 22 '13 at 01:57
  • 1
    Perl has two types of variables, lexical and package (no "d") variables. Lexical variables are lexically-scoped, meaning the can only be seen in the block or file in which they are defined. Package variables are globally-scoped, meaning they can be accessed from anywhere in the program. – ikegami May 22 '13 at 01:59
  • hey man @ikegami, i have a short question for you since you know everything, what does the '\' backslash do in this example: `*{caller( )."::useful"} = \&useful;` – ruggedbuteducated May 22 '13 at 02:58
  • `\&useful` returns a code reference to sub `useful`. As a whole, that line exports `useful` to current sub's caller's package. – ikegami May 22 '13 at 03:24
  • ...similar to \@array, \$scalar, etc, but for subs. – ikegami May 22 '13 at 03:30
2

Here is an answer to both of your questions, emphasis added:

http://www.caveofprogramming.com/perl/perl-variables-%E2%80%94-declaring-and-using-variables-in-perl/

The most important thing to say about variables in Perl, before we get into any further details, is that you should always write use strict and use warnings at the top of your program, then declare all variables with my.

use strict ensures that all variables must be declared with my (or local) before you use them, helping to eliminate typos.

use warnings ensures that your script will warn you about uninitialized variables. Always using strict and warnings will save you a huge amount of time in the long run.

See also:

Why use strict and warnings?

Community
  • 1
  • 1
David Zemens
  • 53,033
  • 11
  • 81
  • 130
  • how does using `my` eliminate typos? – ruggedbuteducated May 22 '13 at 01:41
  • 5
    without `my` perl will declare a variable whenever you use a new variable name. say you have `$name = "hello"`, and you use `$name` in your code. By mistake you use `$nme` and forget the `a`. perl will think you are using a variable `$nme` which is initialized to `null` by default. if you use `my`, perl you give error that variable `nme` is not defined. Thus, eliminate typos and save debugging time. – Bill May 22 '13 at 01:53
  • 3
    That second paragraph of that quote wrong. `local` does not declare variables -- it only works on existing variables -- and won't affect `strict` whatsoever. – ikegami May 22 '13 at 02:04
  • Today I got this that `local` does not declare variables. Thanks @ikegami – serenesat May 05 '15 at 05:27
2

It's good practice to use my since that defines the variable in local scope. Otherwise you'll end up with a mess of global variables which are often the source of bugs.

But if you really don't want to use my, then simply omit the lines use strict and use warnings.

strict vars (perldoc)

This generates a compile-time error if you access a variable that was neither explicitly declared (using any of my, our, state, or use vars ) nor fully qualified. (Because this is to avoid variable suicide problems and subtle dynamic scoping issues, a merely local variable isn't good enough.) See my, our, state, local, and vars.

Community
  • 1
  • 1
ktm5124
  • 11,861
  • 21
  • 74
  • 119