in reply to How to differentiate an empty array from an unitialized one?

with str_three and str_four the resulting array is empty (or may I say "undefined")
No, you may not.

Once you execute my @array =  split ( "," , $_ );, @array has a defined value, period. A variable with an undefined (or uninitialized, for that matter) value is a variable which has either never had a value assigned to it or has had its value explicitly removed. Your split assigns a value to @array, therefore @array is not undefined. (If split could return undef, then @array could potentially end up undefined, but split doesn't do that. Even split ",", undef; returns an empty list, which is a defined value.)

If you want to preserve the empty (not undefined!) values in your input strings, give split a -1 (or any negative integer) as an additional parameter to tell it to keep all values, including leading/trailing empty values:

$ perl -w -E '@values = split ",", "1,2,3,4", -1; say join ",", @value +s' 1,2,3,4 $ perl -w -E '@values = split ",", ",,3,", -1; say join ",", @values' ,,3, $ perl -w -E '@values = split ",", ",,,", -1; say join ",", @values' ,,,

Replies are listed 'Best First'.
Re^2: How to differentiate an empty array from an unitialized one?
by haukex (Archbishop) on Jul 09, 2018 at 09:29 UTC
    @array has a defined value, period. ... therefore @array is not undefined. ... Even split ",", undef; returns an empty list, which is a defined value.

    Sorry, this is incorrect, arrays don't have a concept of "(un)defined", only the elements of an array do - an array is either empty, or not empty. Whether or not the elements of an array are undef or not is a different question - even an array of one undef element is considered "true" by Perl because the array is non-empty.

    $ perl -wMstrict -Mdiagnostics -le 'my @array; print defined(@array)' Can't use 'defined(@array)' (Maybe you should just omit the defined()? +) at -e line 1 (#1) (F) defined() is not useful on arrays because it checks for an undefined scalar value. If you want to see if the array is empty, just use if (@array) { # not empty } for example. Uncaught exception from user code: Can't use 'defined(@array)' (Maybe you should just omit the define +d()?) at -e line 1. $ perl -wMstrict -le 'my @array=(undef); print @array?"true":"false"' true
    Once you execute my @array =  split ( "," , $_ );, @array has a defined value, period.
    $ perl -wMstrict -MData::Dump -e 'my @array; dd @array; $_=""; @array = split(",",$_ ); dd @array' () ()
    If split could return undef ... but split doesn't do that.
    $ perl -wMstrict -MData::Dump -e 'dd split /-|(x)/, "-", -1' ("", undef, "")
      arrays don't have a concept of "(un)defined"
      Yeah, that's an odd design decision which I personally disagree with. I see no logical reason to distinguish between "empty" and "undefined" for scalars, but not for arrays.

      In any case, I considered going into that in my previous comment, but obviously decided against it. I probably should have, given that the literal answer to "How to differentiate an empty array from an uninitialized one?" is "You don't, because Perl doesn't."

      $ perl -wMstrict -MData::Dump -e 'dd split /-|(x)/, "-", -1' ("", undef, "")
      Interesting. I wasn't aware of that behavior in split. Thanks for pointing it out!
        > no logical reason to distinguish between "empty" and "undefined" for scalars

        What do you mean by "empty scalar"? The empty string? Note that it's a perfect value which you can print, measure its length, match against a regex, etc. Undefined value, on the other hand, means the value is missing. If you need to have a special value for "missing value" for the whole array, use an array reference, and assign undef if the whole array is "undefined".

        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,