Professional Web Applications Themes

difference between defined & exists - PERL Miscellaneous

hi all, What is the difference between 'defined' operator and 'exists' operator? Thanks & regards, palani...

  1. #1

    Default difference between defined & exists

    hi all,

    What is the difference between 'defined' operator
    and 'exists' operator?


    Thanks & regards,
    palani
    Palaniappan Guest

  2. #2

    Default Re: difference between defined & exists


    co.in (Palaniappan) wrote: 

    my a;

    $a[0] = 1;

    # defined $a[0] is true
    # exists $a[0] is true

    $a[0] = undef; # or undef $a[0];

    # defined $a[0] is false
    # exists $a[0] is true

    delete $a[0];

    # defined $a[0] is false
    # exists $a[0] is false

    defined applies to any value: it tests if that value is 'undef' or
    not. exists applies to array or hash members: it tests whether there
    is any value, defined or no, there at all.

    Ben

    --
    don't get my sympathy hanging out the 15th floor. you've changed the locks 3
    times, he still comes reeling though the door, and soon he'll get to you, teach
    you how to get to purest hell. you do it to yourself and that's what really
    hurts is you do it to yourself just you, you and noone else * me.uk
    Ben Guest

  3. #3

    Default Re: difference between defined & exists

    co.in (Palaniappan) writes:
     

    Even if you are a newcommer to the Perl community you should still try
    to give as well as take. You have, in fact, already contributed
    something my choosing a good subject line for this thread. Well done,
    that already puts you above the median.

    You could, however, have done more.

    If there is something about the explaination of these operators in the
    reference manual that is unclear then please tell us exactly what.

    Newbies are uniquely qualified to tell us where and how the manual is
    unclear and so help the community improve it. This is something that
    experienced Perl users are unable to do because we know what the
    manual is trying to say already.

    Please take the opportunity afforded you by your _inexperience_ to
    contribute while you can.

    BTW: Don't (yet) bother trying to understand what exists() (and
    delete()) mean when applied to array elements - it's not really
    useful[1].

    The exists() operator is only really useful applied to hash
    elements[2].

    [1] It has to do with pseudo-hashes which are deprocated anyhow.

    [2] And symbolic function references but that's only relevant if you
    are writing AUTOLOAD handlers.

    --
    \\ ( )
    . _\\__[oo
    .__/ \\ /\
    . l___\\
    # ll l\\
    ###LL LL\\
    Brian Guest

  4. #4

    Default Re: difference between defined & exists

    Ben Morrow <me.uk> writes:
     

    Someone with an understanding of the difference between exists() and
    defined() could see that what you just said is a true description.

    On the other hand it probably wouldn't make any sense someone who
    didn't already understand.

    For hashes is easiest to think of exists() as testing if a given key
    is in list of keys of a hash.

    For arrays is easiest to not to think about exists() at all as it
    doesn't really do anything useful.

    To slightly rephrase what Ben said:

    For and array element exists() tests whether there is storage space
    for a scalar value (SV) allocated for that element. Even if there is
    an SV allocated it may still contain undef.

    For most arrays you encounter exists($array[$n]) is true for all $n in
    (-array .. $#array) and false otherwise.

    exists($array[$n]) can also be false if you've used delete() on array
    elements or have extended the array by assigning $#array.

    --
    \\ ( )
    . _\\__[oo
    .__/ \\ /\
    . l___\\
    # ll l\\
    ###LL LL\\
    Brian Guest

  5. #5

    Default Re: difference between defined & exists


    Brian McCauley <com> wrote: 

    Is this so? I hadn't realised. Apologies if I confused the OP:
    pseudohashes are *definitely* something you don't want to know about
    yet (or ever, given that they're going away soon).

    Is exists() not still useful for an array element, with exists($a[$n])
    being equivalent to ($n < a)? (delete() obviously isn't.)

    Ben

    --
    'Deserve [death]? I daresay he did. Many live that deserve death. And some die
    that deserve life. Can you give it to them? Then do not be too eager to deal
    out death in judgement. For even the very wise cannot see all ends.'
    :-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-: me.uk
    Ben Guest

  6. #6

    Default Re: difference between defined & exists

    Ben Morrow <me.uk> wrote: 

    Those aren't exactly equivalent:

    my x;
    $x[1] = undef;
    $#x = 2;

    for (0..3) {
    printf "x[%d] %s\n", $_,
    exists $x[$_] ? "exists" : "doesn't"
    }
    -------
    x[0] doesn't
    x[1] exists
    x[2] doesn't
    x[3] doesn't

    --
    Steve
    Steve Guest

  7. #7

    Default Re: difference between defined & exists


    Steve Grazzini <com> wrote: 
    >
    > Those aren't exactly equivalent:[/ref]

    No, but they ought to be. The only reason they're not is to make
    pseudohashes work.

    Ben

    --
    I've seen things you people wouldn't believe: attack ships on fire off the
    shoulder of Orion; I've watched C-beams glitter in the darkness near the
    Tannhauser Gate. All these moments will be lost, in time, like tears in rain.
    Time to die. |-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-| me.uk
    Ben Guest

  8. #8

    Default Re: difference between defined & exists

    Ben Morrow <me.uk> writes:
     

    It is not equivalent - see other posts in this thread.

    --
    \\ ( )
    . _\\__[oo
    .__/ \\ /\
    . l___\\
    # ll l\\
    ###LL LL\\
    Brian Guest

Similar Threads

  1. Testing if a value exists.
    By Andy Levy in forum PHP Development
    Replies: 3
    Last Post: October 30th, 11:03 AM
  2. method name exists, property value exists, calling method fails
    By Phil Powell in forum PHP Development
    Replies: 0
    Last Post: October 27th, 07:34 PM
  3. exists and defined--not just for hashes anymore
    By Paul Archer in forum PERL Beginners
    Replies: 2
    Last Post: August 27th, 03:33 PM
  4. How to test a hash of arrays for defined/not defined
    By Sisyphus in forum PERL Miscellaneous
    Replies: 1
    Last Post: June 30th, 01:11 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139