defined %hash 
Author Message
 defined %hash

I know that the perldoc for defined warns you about using it on aggregates,
but something seems a little awry.  With 5.004_04 on RH Linux, I was trying
to debug a s{*filter*}of code is a subroutine that looked like:

    my %hash ;
    while (something)
    {
        if (defined %hash) {dosomethingfirsttime}
        ...
    }

What I discovered is that the first time I call that subroutine the defined
test works, but the second time I call the subroutine it doesn't.  Little
test program looked like:

#!/usr/bin/perl

sub t
{
    my %h ;
    warn "Huh?\n" if defined %h ;
    $h{1} = 1 ;

Quote:
}

warn "first call\n" ;
&t ;
warn "second call\n" ;
&t ;

I'd have thought that either I should've gotten two 'huh's or none, no?
The initialization on %h as I come into its scope in &t should've been the
same in both case, shouldn't it??

  /Bernie\
--
Bernie Cosell                     Fantasy Farm Fibers

    -->  Too many people, too few sheep  <--          



Wed, 07 Nov 2001 03:00:00 GMT  
 defined %hash
 [courtesy cc of this posting sent to cited author via email]


:I know that the perldoc for defined warns you about using it on aggregates,
:but something seems a little awry.  

Perhaps I was too coy.  Let me be more clear then:

    DO NOT USE defined() on AGGREGATES.

You will now never again encounter this problem. :-)

--tom
--
It is Unix.  It is possible to overcome any number of these bogus features. --pjw



Wed, 07 Nov 2001 03:00:00 GMT  
 defined %hash

Quote:

>Perhaps I was too coy.  Let me be more clear then:

>    DO NOT USE defined() on AGGREGATES.

Indeed. I think it wouldn't be a bad change to Perl, if defined() used
on hashes and arrays would give a warning, or better still: that it
would be considerd a syntax error.

        Bart.



Fri, 09 Nov 2001 03:00:00 GMT  
 defined %hash

#
# >Perhaps I was too coy.  Let me be more clear then:
# >
# >    DO NOT USE defined() on AGGREGATES.
#
# Indeed. I think it wouldn't be a bad change to Perl, if defined() used
# on hashes and arrays would give a warning, or better still: that it
# would be considerd a syntax error.

I dunno.  If someone wants to rely on behavior that is documented to be
subject to change, I say let them.  Of course, that doesn't help people
who are confused, but those people should read the very clear
documentation.

Of course, under lexical warnings, you could turn off such a warning if it
existed.

--

%PGPKey = ('B76E72AD', [1024, '0824090B CE73CA10  1FF77F13 8180B6B6'])



Fri, 09 Nov 2001 03:00:00 GMT  
 defined %hash


: #
: # >Perhaps I was too coy.  Let me be more clear then:
: # >
: # >    DO NOT USE defined() on AGGREGATES.
: #
: # Indeed. I think it wouldn't be a bad change to Perl, if defined() used
: # on hashes and arrays would give a warning, or better still: that it
: # would be considerd a syntax error.

: I dunno.  If someone wants to rely on behavior that is documented to be
: subject to change, I say let them.  Of course, that doesn't help people
: who are confused, but those people should read the very clear
: documentation.

This is a very bad idea, though it doesn't seem like it on the surface.
Once something's documented, even if marked 'subject to change', you're
stuck with maintaining backwards compatibility for ages. Inevitably
someone'll use it in something mission-critical, and you'll be faced with
the choice of either breaking something big and important, or living with
a substandard feature.

Things should be documented as working or forbidden. Getting wishy-washy
will only end up with annoyed users. (And folks get more annoyed when you
break something you said would break than if you break something you
promised not to. Presumably you get both the 'it broke' annoyance and 'you
caught be doing something I shouldn't have done' annoyance)

Been there, done that. Learned better. Do *not* want to do it again.

                                        Dan



Fri, 09 Nov 2001 03:00:00 GMT  
 defined %hash


# : I dunno.  If someone wants to rely on behavior that is documented to be
# : subject to change, I say let them.  Of course, that doesn't help people
# : who are confused, but those people should read the very clear
# : documentation.
#
# This is a very bad idea, though it doesn't seem like it on the surface.
# Once something's documented, even if marked 'subject to change', you're
# stuck with maintaining backwards compatibility for ages. Inevitably
# someone'll use it in something mission-critical, and you'll be faced with
# the choice of either breaking something big and important, or living with
# a substandard feature.

Hm, good point ... but under lexical warnings, it could be (in theory) an
easily disabled warning.

# Things should be documented as working or forbidden. Getting wishy-washy
# will only end up with annoyed users. (And folks get more annoyed when you
# break something you said would break than if you break something you
# promised not to. Presumably you get both the 'it broke' annoyance and 'you
# caught be doing something I shouldn't have done' annoyance)

Maybe we should add a new keyword, reallydefined().  :)

--

%PGPKey = ('B76E72AD', [1024, '0824090B CE73CA10  1FF77F13 8180B6B6'])



Sat, 10 Nov 2001 03:00:00 GMT  
 defined %hash

Quote:


>>Perhaps I was too coy.  Let me be more clear then:

>>    DO NOT USE defined() on AGGREGATES.

>Indeed. I think it wouldn't be a bad change to Perl, if defined() used
>on hashes and arrays would give a warning,

Coincidentally, I just sent in a patch that does exactly this.

Quote:
> or better still: that it would be considerd a syntax error.

No, that would be bad.  You can't tell someone with a working program
that has run for ten years that their program suddenly has a syntax
error for no reason at all.


Sun, 11 Nov 2001 03:00:00 GMT  
 defined %hash


        >snip<
:>Indeed. I think it wouldn't be a bad change to Perl, if defined() used
:>on hashes and arrays would give a warning,
:
: Coincidentally, I just sent in a patch that does exactly this.

        Great, thanks.

:> or better still: that it would be considerd a syntax error.
:
: No, that would be bad.  You can't tell someone with a working program that
: has run for ten years that their program suddenly has a syntax error for
: no reason at all.

        Umm...too late:





        Execution of -e aborted due to compilation errors.

--

                                        Pizza......for the body.
                                        Sushi......for the soul.
                                             -- User Friendly



Tue, 13 Nov 2001 03:00:00 GMT  
 defined %hash


# : No, that would be bad.  You can't tell someone with a working program that
# : has run for ten years that their program suddenly has a syntax error for
# : no reason at all.
#
#         Umm...too late:
#




#         Execution of -e aborted due to compilation errors.

perl 5 came out in 1994, perl 1 in 1987.  It was less than seven years,
not 10 years.

--

%PGPKey = ('B76E72AD', [1024, '0824090B CE73CA10  1FF77F13 8180B6B6'])



Tue, 13 Nov 2001 03:00:00 GMT  
 defined %hash

Quote:

> : No, that would be bad.  You can't tell someone with a working program that
> : has run for ten years that their program suddenly has a syntax error for
> : no reason at all.

Zenin:

Quote:
>    Umm...too late:




Yes, but that isn't `for no reason at all'.  It's for a very good
reason, and it wasn't done lightly; I discuss this at length at

        http://www.plover.com/~mjd/perl/at-error.html


because we feel like it ought to be, which is what Bart Lateur was
suggesting.

I don't know what you think your point is, unless maybe it was to say
that I was wrong, which I wasn't.  So perhaps next time you have one
you could try phrasing it in a less oblique way.



Tue, 13 Nov 2001 03:00:00 GMT  
 defined %hash
        >snip<
: Zenin:
:>   Umm...too late:
:>


:
: Yes, but that isn't `for no reason at all'.  It's for a very good
: reason, and it wasn't done lightly; I discuss this at length at
:       http://www.plover.com/~mjd/perl/at-error.html

        Agreed, but that's not the point.


: because we feel like it ought to be, which is what Bart Lateur was
: suggesting.


        ifs, ands, or buts.  It just isn't cought by the compiler as it
        should and can be.


        the compiler and runtime as well, simply to go off and do whatever
        it may like this particular day of the week?

        For that matter, why have prototypes at all?  Why not let everyone
        call any builtin any way they see fit and have perl grok it in any
        random way it feels like?

        No, I don't think so.  I like having a net under my tightrope, thank
        you very much.

: I don't know what you think your point is,

        Quite simply that defined(LIST) *IS* currently an error, and should
        be flagged as such, period.

: unless maybe it was to say that I was wrong, which I wasn't.

        :-/

: So perhaps next time you have one you could try phrasing it in a less
: oblique way.

        No problem, sir.

        "I'm sorry, but you just made me lose my sense of humor, which is
         deeply regrettable."

--

                                        Pizza......for the body.
                                        Sushi......for the soul.
                                             -- User Friendly



Wed, 14 Nov 2001 03:00:00 GMT  
 defined %hash
Where does one find this "very clear documentation?" Most of the documentation I
have read seems to be in what I call "reference manual mode." Meaning, the
person who wrote the docs obviously knows what it is s/he is trying to explain,
and writes as if the reader knows also, but apparently forgot it and only needs
the docs to refresh his memory. I have spent many an hour reading the docs
saying to myself "I understand what it says, but what on Earth does it mean?"

--Chuck

Quote:




> #
> # >Perhaps I was too coy.  Let me be more clear then:
> # >
> # >    DO NOT USE defined() on AGGREGATES.
> #
> # Indeed. I think it wouldn't be a bad change to Perl, if defined() used
> # on hashes and arrays would give a warning, or better still: that it
> # would be considerd a syntax error.

> I dunno.  If someone wants to rely on behavior that is documented to be
> subject to change, I say let them.  Of course, that doesn't help people
> who are confused, but those people should read the very clear
> documentation.

> Of course, under lexical warnings, you could turn off such a warning if it
> existed.

> --

> %PGPKey = ('B76E72AD', [1024, '0824090B CE73CA10  1FF77F13 8180B6B6'])



Wed, 14 Nov 2001 03:00:00 GMT  
 defined %hash

Quote:

> Where does one find this "very clear documentation?" Most of the documentation I
> have read seems to be in what I call "reference manual mode." Meaning, the
> person who wrote the docs obviously knows what it is s/he is trying to explain,
> and writes as if the reader knows also, but apparently forgot it and only needs
> the docs to refresh his memory. I have spent many an hour reading the docs
> saying to myself "I understand what it says, but what on Earth does it mean?"

While I wouldn't recommend learning Perl by typing "perldoc -f xyzzy"
for various values of "xyzzy()", this *is* in the manual.

The correct "documentation reading mode" for this, like most other
UNIX manuals, seems to be "read the description of what it does, note
that what it does doesn't make sense in the case of aggregates (undef
doesn't make sense as a value for an aggregate), see that the manual
indeed says that using it won't make sense, and (if I can be bothered
to read to the end) read a more technical explanation of what it
really does for aggregates.

I guess it boils down to whether you have in mind some "reasonable"
definition for defined() on an aggregate.

--

Compugen Ltd.          |Tel: +972-2-6795059 (Jerusalem) \  NEW IMPROVED URL!
72 Pinhas Rosen St.    |Tel: +972-3-7658520 (Main office)`--------------------
Tel-Aviv 69512, ISRAEL |Fax: +972-3-7658555  http://3w.compugen.co.il/~ariels



Thu, 15 Nov 2001 03:00:00 GMT  
 defined %hash

: Where does one find this "very clear documentation?"

   In the description for the function being used.

   Doesn't take a Rocket Scientist to figure that you should
   probably read up on what defined() does when you decide to
   use the defined() function...

      perldoc -f defined

      "use of C<defined()> upon aggregates (hashes and arrays)
       is not guaranteed to produce intuitive results, and
       should probably be avoided."

   and

      "This counterintuitive behavior of C<defined()> on
       aggregates may be changed, fixed, or broken in a
       future release of Perl."

   The workaround for that is given a few lines down:

      "You should instead use a simple test for size:"

   All of those are easily found by doing a word search for
   "aggregate".


: >

: > #
: > # >Perhaps I was too coy.  Let me be more clear then:
: > # >
: > # >    DO NOT USE defined() on AGGREGATES.

: > I dunno.  If someone wants to rely on behavior that is documented to be
: > subject to change, I say let them.  Of course, that doesn't help people
: > who are confused, but those people should read the very clear
: > documentation.

--
    Tad McClellan                          SGML Consulting

    Fort Worth, Texas



Thu, 15 Nov 2001 03:00:00 GMT  
 defined %hash

Can I try to clarify my original question a tiny bit...  I originally wrote
the 'defined %hash' without thinking much about it [seemed to be the
logical thing to do, indeed: compare my view on that with my comments in
the thread on "uninitialized variables" on c.l.p.misc].  Yes, I looked in
the manual, and yes, I fixed my code and all's well.

What I was curious about was:






In the little test code I supplied back at the beginning of this thread, I
was surprised that the first time through the loop the 'my' %hash was "not
defined", but the second time through it was.  Now, if defined(aggregates)
was specified to be arbitrary, unpredictable or random (for a single
instance of Perl.. I understand about the behavior changing between
versions), then I would have no question about this behavior, but I was
actually asking a question about the initialization of 'my' aggregates as
you enter their scope:

Clearly, there was _some_ difference in the set up of my %hash between the
first and second times entering the block.  I was wondering just what this
difference was, and if there might be any other "legit" way [perhaps by
taking a 'ref' to it or something like that] to detect the difference
between the two that could conceivably cause other problems...

  /Bernie\
--
Bernie Cosell                     Fantasy Farm Fibers

    -->  Too many people, too few sheep  <--          


Fri, 16 Nov 2001 03:00:00 GMT  
 
 [ 31 post ]  Go to page: [1] [2] [3]

 Relevant Pages 

1. defined (defined(%hash))

2. alternatives to defined($hash{}) ?

3. User-defined hash...

4. read-accessing hash element implicitely defines hash, or I found a bug?

5. How to test a hash of arrays for defined/not defined

6. Local hashes defined() in perl5?

7. why key is added to hash after defined ?

8. Using the defined operator on a hash

9. defined() upon aggregates (hashes and arrays) is not guaranteed to produce intuitive results

10. set up hash using variables not yet defined

11. Hashes - defined or not?

12. Defining a global hash ref ...

 

 
Powered by phpBB® Forum Software