LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


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

Rep: Reputation: 15
Question undefined variable increments in PERL


Hi,

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

Try,

$i++;
print $i;


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.

If this is expected, why should this be so?

Can somebody help me unerstanding this plzzz?



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

Rep: Reputation: 60
From perldoc perlsyn
Quote:
Declarations

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.
 
Old 09-12-2008, 07:11 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.5
Posts: 17,670

Rep: Reputation: 2487Reputation: 2487Reputation: 2487Reputation: 2487Reputation: 2487Reputation: 2487Reputation: 2487Reputation: 2487Reputation: 2487Reputation: 2487Reputation: 2487
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 prog.pl
 
Old 09-18-2008, 09:46 AM   #4
gaynut
LQ Newbie
 
Registered: Jan 2008
Posts: 27

Original Poster
Rep: Reputation: 15
Telemachos,chrism01,

Ur reply explains me things clearly.
thanks.






~ gaynut
 
  


Reply


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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:04 AM.

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