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 following 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With