Dereferencing the empty hash ref constructor 
Author Message
 Dereferencing the empty hash ref constructor

spindrift> perl -we 'my %a = %{};'
syntax error at -e line 1, near "%{"

spindrift> perl -we 'my %a = %{ {} };'
Unmatched right curly bracket at -e line 1, at end of line
syntax error at -e line 1, near "%{ {} }"

spindrift> perl -we 'my %a = %{ 0||{} };'
 ... works!!!

?????

(perl 5.6.0)

--




Sat, 26 Oct 2002 03:00:00 GMT  
 Dereferencing the empty hash ref constructor

Quote:

> spindrift> perl -we 'my %a = %{ 0||{} };'
>  ... works!!!

To force something too look like data, (i.e. in this case a
hash ref), you can usually put a plus sign in front of it:

    my %a = %{ +{} };

Although somewhat unsightly, this is probably the best way to
do what you meant to do.

(But what's wrong with my %a = ();  or even just my %a; ???)

--
John Porter



Sun, 27 Oct 2002 03:00:00 GMT  
 Dereferencing the empty hash ref constructor

Quote:

> spindrift> perl -we 'my %a = %{};'
> syntax error at -e line 1, near "%{"

No great surprise here.  % dereferences a following simple scalar or
BLOCK.  The syntax seems to reject an empty block here.

Quote:
> spindrift> perl -we 'my %a = %{ {} };'
> Unmatched right curly bracket at -e line 1, at end of line
> syntax error at -e line 1, near "%{ {} }"

I don't get this error w/ my 5.6.0.  Note that that inner {} could be
ambiguous (either an anonymous hash reference or a nested block).  In
practice, it doesn't seem to allow a nested block in %{...}.

Quote:
> spindrift> perl -we 'my %a = %{ 0||{} };'
>  ... works!!!

This disambiguates the {}.  It can only be an anonymous hash now.
%{ +{} } should work too.

%{ {;} } should force it to be a block, but doesn't work.
But I can't think of any actual reason to put a nested block in.



Sun, 27 Oct 2002 03:00:00 GMT  
 Dereferencing the empty hash ref constructor

Quote:

> spindrift> perl -we 'my %a = %{ {} };'
> Unmatched right curly bracket at -e line 1, at end of line
> syntax error at -e line 1, near "%{ {} }"

I don't know why this fails, but you can fix it by forcing Perl to
interpret the inner curlies as a hash constructor:

        %{ +{} }

        %{ ({}) }

Quote:
> spindrift> perl -we 'my %a = %{ 0||{} };'
>  ... works!!!

Ugh!  :-)

Markus

--
Web site of the week: <http://www.mailexpire.com/>



Sun, 27 Oct 2002 03:00:00 GMT  
 Dereferencing the empty hash ref constructor
Thanks for the replies. I appreciate it. Just one quick follow-up then
I'll return to lurking.

Quote:

> > spindrift> perl -we 'my %a = %{};'

> (But what's wrong with my %a = ();  or even just my %a; ???)

That's on a need to know basis, and you have no need to know. :-)

Well, OK. This is a distillation of a larger problem involving ways to
expand a hash reference which may actually be undefined. I tried things
like

sub foo {
  my $opts=shift;
  my %allopts = %{ %{$opts || {}}, %{$global_opts->{foo} || {}} };
...

Quote:
}

I don't remember whether the exact construction above worked or not, but
I ran into difficulties with some formulations of this solution. What I
chose to do is working fine, but I became curious about why the syntax
like that quoted at the top failed.

Quote:

>> spindrift> perl -we 'my %a = %{};'
>> syntax error at -e line 1, near "%{"

>No great surprise here.  % dereferences a following simple scalar or
>BLOCK.  The syntax seems to reject an empty block here.

Well it was a surprise to me. I could't think of a reason why a '{}' in
this context shouldn't be treated as a ref to an empty hash and then
dereferenced. That's just the way the parser bounces. It's not a bug,
but I thought it was perplexing.

Quote:
>> spindrift> perl -we 'my %a = %{ {} };'
>> Unmatched right curly bracket at -e line 1, at end of line
>> syntax error at -e line 1, near "%{ {} }"

>I don't get this error w/ my 5.6.0.

If only some implementations can parse it, is it a bug? Does this need
to be elevated to perl-porters? What can I check to rule out a screw-up
at my end?

--




Sun, 27 Oct 2002 03:00:00 GMT  
 Dereferencing the empty hash ref constructor

Quote:

> Well, OK. This is a distillation of a larger problem involving ways to
> expand a hash reference which may actually be undefined. I tried things
> like

> sub foo {
>   my $opts=shift;
>   my %allopts = %{ %{$opts || {}}, %{$global_opts->{foo} || {}} };

Seem like you want something like

my %allopts;

Graham.



Tue, 29 Oct 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Problem using InfoPower with Queries

2. COPY/PASTE RECORD

3. useless use of scalar ref constructor

4. Dereferencing hash within hash

5. Obtaining a ref to a hash using an element of the hash

6. making an array to hash ref of hash refs

7. hash vs hash ref

8. using filename from producedure

9. Announce: Data Management System for Pascal

10. Borland Turbo Pascal 7

11. Help Interbase 16 to 32

12. graph unit

 

 
Powered by phpBB® Forum Software