Solved

what is the difference between the usageof exits and defined fuction in perl?

Posted on 2011-09-24
5
202 Views
Last Modified: 2012-05-12
Hello, what is the difference between the usageof exits and defined fuction in perl? I would like to understand on which scenario one should be used and not the others. Please provide some example to understand it better. Thanks!
0
Comment
Question by:beer9
  • 3
  • 2
5 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
ID: 36592172
$hash{key}=undef;
print "Defined\n"   if defined $hash{key};
print "Exists\n"    if exists $hash{key};

$scalar=1;
print "Defined\n"   if defined $scalar;
eval 'print "Exists\n"    if exists $scalar';
print $@;

See also
perldoc -f exists
perldoc -f defined
0
 
LVL 26

Assisted Solution

by:wilcoxon
wilcoxon earned 250 total points
ID: 36592547
To expand on ozo's example code...

exists only works on hashes and says if the key exists (in the example above $hash{key} exists but $hash{other} does not).  Put another way, it queries the %hash itself asking does "key" exist as a key in the hash?

Defined simply says if the value is defined or not (neither $hash{key} nor $hash{other} are defined in the above).  It will work on things other than a hash.  Put another way, for hashes, it asks is the specific value of $hash{key} defined?

One thing that surprised me a little that I just tested is that defined($hash{other}) does not auto-vivify (auto-magically create) $hash{other}.  In other words, defined($hash{other}) followed by exists($hash{other}) both return false (however either defined($hash{other}{sub}) or exists($hash{other}{sub}) will auto-vivify so exists($hash{other}) after either will return true).
0
 
LVL 84

Expert Comment

by:ozo
ID: 36594047
As explained in the the Perl documentation cited above
exists also works on array elements and subroutine names
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 36594129
Interesting.  I still learn new things about perl.  I never knew (or forgot) that exists will work for array elements or subroutine names.  I can think of cases when it would be useful to test array elements but can't think of any time when it would have been useful for subroutine names (but I'm sure they exist in some use cases).
0
 
LVL 84

Expert Comment

by:ozo
ID: 36594160
perldoc Errno
has an example of the use of exists on a subroutine name

the B module also uses it
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now