in reply to Re: if (defined @arr) problem
in thread if (defined @arr) problem

That's because the array contains one value : 'undef' .
That looks like a string with the text "undef" to me. That's not what the array contains. Instead, it holds an undefined scalar value.

Look at what happens if you do :
sub test { return () ; }

Lo and behold, it works!

Still, testing definedness of an array is a very bad idea. Try running this code first:

@a = qw(a b c);
and now you'll see, the array will be defined (and empty).

Don't. Do. It.

Definedness of an array is nothing a newbie should ever be concerned about.

Replies are listed 'Best First'.
Re^3: if (defined @arr) problem
by secret (Beadle) on Dec 15, 2005 at 10:49 UTC
      Still, testing definedness of an array is a very bad idea. Try running this code first:
      @a = qw(a b c);
    Ah yes :) that's very true !  scalar @array is, of course, much better !

    ( i did meant the undef value, btw, and not the string 'undef' but it's true that my use of single quotes was indeed a bad idea !! )

    I'll add something : suppose the OP wanted to undef arrays in a sub . I think I would pass the array as a reference, undef it in the sub and return the reference .

      I'll add something : suppose the OP wanted to undef arrays in a sub . I think I would pass the array as a reference, undef it in the sub and return the reference.

      In technical discussions terms have precise meanings and it's important to use them in the same way that other people do. I don't think that "undef an array" means what you think it means.

      If you mean to remove all of the elements of the array, then say that. If you want to replace all of the elements with the value undef then say that. Or do you actually mean that you want to call undef @ARRAY? According to the docs that will "forget @ARRAY ever existed". Which of these do you mean?

      --
      <http://dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

          remove all of the elements of the array

        Well that would be @array = ()

          replace all of the elements with the value undef

        That could be @a = (undef) x scalar @a

        So yes , saying "undef an array" i meant  undef @a . I do think it's what the OP was meaning to do when trying to affect one single undef value to the whole array . This really sounds like undef @array.

        Now about delete for the moment i never got to use it, neither did i use exist . I gather it's because they are used mainly with objects, are they ?

        Regards,
        secret

      Ah yes :) that's very true ! scalar @array is, of course, much better !
      And while we're here, it's worth reminding that if you use @array in scalar context, then an explicit call to scalar is not needed:
      D'Oh unless @array; # Valid Perl!!
Re^3: if (defined @arr) problem
by parv (Parson) on Dec 15, 2005 at 16:58 UTC
    > Still, testing definedness of an array is a very bad idea.
    > Try running this code first: 
    >
    > @a = qw(a b c);
    >
    > and now you'll see, the array will be defined (and empty).
    

    Hmm, i get "defined: yes" and "size: 3" ...

    @a = qw/a b c/; printf "defined: %s\nsize: %d\n" , defined @a ? 'yes' : 'no' , scalar @a ;

    ... what did i miss?

      You missed plugging it into the whole script. I said "do this first", didn't I?

      Well, apparently that wasn't clear, so here is the whole current script, as I see it.

      #! /usr/bin/perl @a = qw/a b c/; @a = test(); printf "defined: %s\nsize: %d\n" , defined @a ? 'yes' : 'no' , scalar +@a ; sub test { return () ; }
        Yes, you certainly mentioned "do(ing) it first". I was not sure what or where exactly you meant for the behaviour shown (in the complete program given above) is what /i/ already expect. Thanks for the clarification.