Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-12-2008, 05:42 AM   #1
LQ Newbie
Registered: Jan 2008
Posts: 27

Rep: Reputation: 15
Question undefined variable increments in PERL


I'm just not clear about the variables in PERL.


print $i;

on the CLI or in a program.
It prints the output as 1

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.

If this is expected, why should this be so?

Can somebody help me unerstanding this plzzz?

~ gaynut
Old 09-12-2008, 06:45 AM   #2
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
From perldoc perlsyn

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;

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:

Last edited by Telemachos; 09-12-2008 at 06:53 AM.
Old 09-12-2008, 07:11 AM   #3
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 17,802

Rep: Reputation: 2540Reputation: 2540Reputation: 2540Reputation: 2540Reputation: 2540Reputation: 2540Reputation: 2540Reputation: 2540Reputation: 2540Reputation: 2540Reputation: 2540
Personally I recommend starting all Perl progs with

#!/usr/bin/perl -w
use strict; # Enforce declarations

ie turn on warnings and strict. They'll save you from a lot of issues. Also, to check a perl prog without running it use:

perl -wc
Old 09-18-2008, 09:46 AM   #4
LQ Newbie
Registered: Jan 2008
Posts: 27

Original Poster
Rep: Reputation: 15

Ur reply explains me things clearly.

~ gaynut


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl:Not able to execute command in variable jags.singh Programming 1 07-23-2007 07:51 AM
[Perl] wget in variable noir911 Programming 3 04-30-2007 06:39 AM
perl @INC variable nodger Programming 3 07-10-2006 11:17 PM
Perl undefined subroutine /can't locate object method cobien Programming 1 11-17-2003 10:47 AM
Need Perl/CGI help! Undefined Subroutine CragStar Programming 4 03-05-2002 07:28 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:53 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration