Re: Autovivification by foreach



Frank Seitz <devnull4711@xxxxxx> wrote in
news:71udaaFn4bctU1@xxxxxxxxxxxxxxxxxx:

Uri Guttman wrote:
"FS" == Frank Seitz <devnull4711@xxxxxx> writes:

FS> my (@a,$ref);
FS> push @a,@$ref;

FS> Perl throws an exception:
FS> Can't use an undefined value as an ARRAY reference
FS> Seems plausible.

FS> We are modifying the code:

FS> my (@a,$ref);
FS> push @a,$_ for @$ref;

FS> Now, there is no exception anymore. Perl creates the array on
the fly. FS> Why is it so? What is the difference?

in the first case @$ref is readonly as it is input to push and can't
be modified. autovivification only happens on lvalues when they are
undef and used where a ref should be.


....

This explanation is not plausible to me. Perl autovivifies before
loop start, because the loop might be modifying array elements?
How is this possible? The autovivificated array is empty.
It is clear that the body of the loop is never executed.

In my opinion it would make more sense when Perl would
throw an exception in this case too.

First, note that I do think this behavior is a little quirky.

However, it seems to me that autovivification in this context is useful.

It allows you to avoid writing code to check $ref before, say, adding
elements to some array for further processing. In that context, one may
not care about the definedness of $ref per se but only about if it
points to further elements to process.

Uri's explanation made sense to me. Hash element autovivification has
its quirks too but it is useful under similar circumstances.

If you don't want autovivification and you want to avoid the exception,
you can write:

push @a, @$ref if ref $ref eq 'ARRAY';

Or, if you want the exception, write:

die '$ref is not defined' unless defined $ref;
die '$ref is not an array ref' unless ref $ref eq 'ARRAY';

Sinan

--
A. Sinan Unur <1usa@xxxxxxxxxxxxxxxxxxx>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
.



Relevant Pages

  • Re: Autovivification bei for
    ... hat man mich darauf aufmerksam gemacht, dass Autovivification ... leeren Array natürlich nicht passiert. ... zu keiner Exception, ...
    (de.comp.lang.perl.misc)
  • Re: Interop, Late Binding and Array of longs
    ... C# type with no more success (i have the same exception) ... How would you pass by reference an array between C# and VB6 ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: regex question - trying to find ".mp3" in a SELECT box
    ... But `Array' is a Function object reference. ... Establish a new execution context using F's FormalParameterList, ... Since is not a primitive value, the exception should be thrown. ... | 5.2 Algorithm Conventions ...
    (comp.lang.javascript)
  • Re: An isArray test (and IE bugs)
    ... is not 'Array' must not be an Array, ... You have no choice but to catch every exception that is thrown in a - try - block, but only the exceptions that you expected to be catching should be handled in the - catch - block. ... That suggests that each - catch - block should contain the code to identify the exact type and nature of the exception thrown so that all the exceptions that you did not expect to be catching can be re-thrown. ... So while you could examine the strings in an given set of browsers/browser configurations you are going to hard pressed to be in a position to identify the exception in a browser that you have never seen. ...
    (comp.lang.javascript)
  • Re: extract matching vales
    ... Ref no spaces in list & unique ... Name No spaces in list but has duplicates but not within the same package ref ... I used a "psuedo" error trap that effectively does the same thing but is ... This will return an array of TRUE's or FALSE's. ...
    (microsoft.public.excel.misc)