in reply to Check if array is null
Is there any way to check if an array has been initialized? I am executing a command and that output is going into an array. If the command has no output then the array does not get initialized. Is there a way to check that?Your assumptions are wrong:
after
is run, @array1 and @array2 will both be initialized. @array1 will be empty: you can check for that using:my @array1; my @array2 = some_code();
if (@array1) { print "not empty\n"; }
@array2 will contain whatever some_code() returns. some_code() will allways return a list, because it's called in list context (caused by the assignment to @array2).
If some_code returns like this:
@array2 will be empty, like @array1 above.return (); # an empty list
If some_code returns like this:
@array2 will contain 1 element which will be undefined.return undef; # undefined scalar
If some_code returns like this:
return; # "return false"
@array2 will be empty (return; without arguments returns an empty list in list context, and undef in scalar context)
If some_code does not use a return statement, the last statement in some_code will be executed in list context and its value will be returned.
|
|---|