develooper Front page | perl.perl5.porters | Postings from February 2012

proposal on defined and exists

Thread Next
From:
Reini Urban
Date:
February 16, 2012 12:28
Subject:
proposal on defined and exists
Message ID:
CAHiT=DGpjwkzqQeRHuBh7N9SAQeXc-c1ES=qHqX_wbUcpzFaHA@mail.gmail.com
I think we should adopt `exists` for checking existance of global, lexical and
local symbols - more than just "variables" - and all types with
non-autovivification
of symbols.
This would clean up the difficulties with defined and help in the case of
autovivification and lexicals. Think of lexical gensym's.
It is also misleading that even the core testsuite in t/op/gv.t tests
for defined() not to create the symbolic arguments.
`# test if defined() doesn't create any new symbols`

non-vivify           vififies *symbol
exists(*symbol) <==> defined *{symbol}{GLOB}   // currently nonsense
exists($scalar)  <==> defined *{symbol}{SCALAR} // currently nonsense
exists(@array)  <==> defined *{symbol}{ARRAY}
exists(%hash)  <==> defined *{symbol}{HASH}
exists(&sub)     <==> defined *{symbol}{CODE}

exists $hash{key}  = keep established meaning
exists $hash->{key}    -"-
exists $array[key] = keep established meaning with caveat for delete,
                     should be different to defined $array[key]
                     exists can be checked for deletion.
exists $array->[key]   -"-

vivification of interim hash elements in a chain stays untouched, i.e.
exists $hash{key}->{element} would still create $hash{key} unless
`no autovivification` is in effect.

This should work for lexicals and locals also:
{ my @array; exists(@array) }
{ local @arrray; exists(@array) }

magical symbols are not guaranteed to non-autovivification since
they already exist.

Note that currently even these fails:
perl -le'{no autovivification; print 1 if defined *{'xx'}{GLOB};}'
*xx is still created.
perl -le'{no autovivification; print 1 if defined *{'xx'}{SCALAR};}'
*xx is still created.

May I write a patch for this?
-- 
Reini Urban
http://cpanel.net/   http://www.perl-compiler.org/

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About