ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
on the CLI or in a program.
It prints the output as 1
Query:
Notice that i haven't declared/defined the variable.
It is not assined to any value even zero.
In that case, when i try to some arithmetic on a variable that is undefined , shouldn't the interpreter throw some error.
The only things you need to declare in Perl are report formats and
subroutines (and sometimes not even subroutines). A variable holds the
undefined value ("undef") until it has been assigned a defined value,
which is anything other than "undef". When used as a number, "undef"
is treated as 0; when used as a string, it is treated as the empty
string, ""; and when used as a reference that isn't being assigned to,
it is treated as an error. If you enable warnings, you'll be notified
of an uninitialized value whenever you treat "undef" as a string or a
number. Well, usually. Boolean contexts, such as:
my $a;
if ($a) {}
are exempt from warnings (because they care about truth rather than
definedness). Operators such as "++", "--", "+=", "-=", and ".=", that
operate on undefined left values such as:
my $a;
$a++;
are also always exempt from such warnings.
Perl ain't C (or many other languages). You don't need to predeclare $i. When you apply ++ to $i, Perl assumes you know what you are doing. It moves $i from 0 (treating it as a number, undef = 0) and then increments it to 1.
Edit: if you have Perl,you almost certainly have perldoc installed (try perldoc perldoc and perldoc perltoc for an intro to the documentation and its structure), but you can also find it online here: http://perldoc.perl.org/
Last edited by Telemachos; 09-12-2008 at 06:53 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.