develooper Front page | perl.perl5.porters | Postings from August 2021

Re: defined $a, where $a is actually a HASH

Thread Previous | Thread Next
From:
Tony Cook
Date:
August 20, 2021 00:06
Subject:
Re: defined $a, where $a is actually a HASH
Message ID:
20210820000611.GZ15367@venus.tony.develop-help.com
On Thu, Aug 19, 2021 at 04:35:00PM -0500, David Nicol wrote:
> > :    These have been unreachable since `defined @array` and `defined %hash`
> > :    became syntax errors.
> >
> 
> 
> looks like I missed several meetings. I still expect
> 
>     no strict;
>     print (defined(@arr) ? 1 : 2);
>     @arr = (1);
>     print (defined(@arr) ? 1 : 2);
>     print (defined(%h) ? 1 : 2);
>     %h = (1,2);
>     print (defined(%h) ? 1 : 2);
>     print "\n";
> 
> to print 2121.
> 
> I have apparently never needed to do that, but the new thing is something
> like *h->{HASH}, right?

C<< defined AGGREGATE >> returned information about the internal state
of the aggregate, and wasn't about whether there was a glob or not.

It worked for lexicals:

$ ~/perl/5.10.0-debug/bin/perl -le 'my @x; print defined @x; @x = 1; print defined @x; @x = (); print defined @x'

1
1

but it was misleading, since while lexicals do get initialized when
introduced, that internal state isn't reset:

$ ~/perl/5.10.0-debug/bin/perl -le 'for (1,2) { my @x; print defined @x; @x = 1; print defined @x; @x = (); print defined @x; }'

1
1
1
1
1

The equivalent would be something like:

  use B;
  my $x = B::svref_2object(\@x);
  my $def = $x->MAX >= 0;

but why do that?

Tony

Thread Previous | 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