In Perl, how can I use Tie::IxHash with a dictionary while 'use strict' is on?
I'm trying to create a hash that preserves the order that the keys are added. Under section "Create a hash and preserve the add-order" of this page, it gives a snippet that modifies a hash so when you do keys
it returns the keys in the order that you inserted them into the hash.
When I do the fol开发者_如何学Golowing snippet:
use strict;
our %foo;
use Tie::IxHash;
tie (%foo, Tie::IxHash);
It fails with:
Bareword "Tie::IxHash" not allowed while "strict subs" in use at /nfs/pdx/home/rbroger1/tmp.pl line 4.
Execution of /nfs/pdx/home/rbroger1/tmp.pl aborted due to compilation errors.
How can I get Tie::IxHash to work when use strict
is on?
dsolimano's Example worked.
use strict;
our %foo;
use Tie::IxHash;
tie (%foo, "Tie::IxHash");
$foo{c} = 3;
$foo{b} = 2;
$foo{a} = 1;
print keys(%foo);
prints:
cba
without the tie...Tie::IxHash
line it is
cab
The second argument to tie
is a string, so try
use strict;
our %foo;
use Tie::IxHash;
tie (%foo, 'Tie::IxHash');
Using quotes eliminates the error:
use strict;
our %foo;
use Tie::IxHash;
tie (%foo, "Tie::IxHash");
It is not mentioned in the POD, but it is used in the examples on CPAN.
See also tie.
精彩评论