in reply to Re^2: when is "my" not necessary?
in thread when is "my" not necessary?
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);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: when is "my" not necessary?
by argv (Pilgrim) on Nov 06, 2004 at 06:22 UTC | |
by runrig (Abbot) on Nov 17, 2004 at 00:37 UTC | |
by demerphq (Chancellor) on Nov 17, 2004 at 21:54 UTC |