Well...
my $hash; $hash{foo} = bar;
If you meant:
my %hash; $hash{foo} = 'bar'; #Versus my $hash = {}; $hash->{foo} = 'bar';
Then there is still a difference. In the first, you create a lexical hash named %hash, then assign a value to one of it's keys. In the second you create a lexical scalar which contains an anonymous reference to a hash.
To examine the difference, run this:
Note how the variable in the second case "points to" (is a reference to) the actual hash, while the first case is just the hash. I highly suggest reading up on references, and man perlvar -- the nuances are important and often incredibly useful. ;-)#!/usr/bin/perl use strict; use warnings; use Data::Dumper; print "A lexical hash:\n"; my %hash; $hash{foo} = 'bar'; print Dumper(\%hash); print "A lexical hash *reference*:\n"; my $hash = {}; $hash->{foo} = 'bar'; print Dumper(\$hash);
In reply to Re^3: when is "my" not necessary?
by radiantmatrix
in thread when is "my" not necessary?
by argv
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |