Iterating hash based on the insertion order?

Hashes are unordered by default in Perl 5. You can use tie and Tie::IxHash to override this behavior. Be warned though, there is a performance hit and other considerations (like the fact that the order will not be preserved in copies). #!/usr/bin/perl use strict; use warnings; use Tie::IxHash; tie my %hash, “Tie::IxHash” or die “could … Read more

What is the difference between new Some::Class and Some::Class->new() in Perl?

Using new Some::Class is called “indirect” method invocation, and it’s bad because it introduces some ambiguity into the syntax. One reason it can fail is if you have an array or hash of objects. You might expect dosomethingwith $hashref->{obj} to be equal to $hashref->{obj}->dosomethingwith(); but it actually parses as: $hashref->dosomethingwith->{obj} which probably isn’t what you … Read more

Global symbol requires explicit package name

Have a look at perldiag: Global symbol “%s” requires explicit package name (F) You’ve said “use strict” or “use strict vars”, which indicates that all variables must either be lexically scoped (using “my” or “state”), declared beforehand using “our”, or explicitly qualified to say which package the global variable is in (using “::”).

How can I combine hashes in Perl?

Quick Answer (TL;DR) %hash1 = (%hash1, %hash2) ## or else … @hash1{keys %hash2} = values %hash2; ## or with references … $hash_ref1 = { %$hash_ref1, %$hash_ref2 }; Overview Context: Perl 5.x Problem: The user wishes to merge two hashes1 into a single variable Solution use the syntax above for simple variables use Hash::Merge for complex … Read more

Getting STDOUT, STDERR, and response code from external *nix command in perl

This was exactly the challenge that David Golden faced when he wrote Capture::Tiny. I think it will help you do exactly what you need. Basic example: #!/usr/bin/env perl use strict; use warnings; use Capture::Tiny ‘capture’; my ($stdout, $stderr, $return) = capture { system( ‘echo Hello’ ); }; print “STDOUT: $stdout\n”; print “STDERR: $stderr\n”; print “Return: … Read more