1234567891011121314151617181920212223242526272829303132333435 |
- #!/usr/bin/env perl
- # Implements say command without Modern::Perl or anything extra.
- # Should support below Perl 5.10 comfortably. Supports variables, arrays and simple key=>value hashes (with a quirk).
- # Quirk: Prints hash element keys and values each in a new line. As a workaround, add a \ before the hash name. e.g. say \%arr2;
- # @source https://stackoverflow.com/a/31777816
- unless (eval "defined(&say)") {
- sub say {
- my %hash = %{$_[0]};
- if (scalar %hash > 0) { foreach (sort keys %hash) { print " $_ => $hash{$_}", "\n"; } }
- elsif (@_) { foreach (@_) { print $_, "\n"; } }
- else { print $_, "\n"; }
- }
- }
- # Examples:
- my $test='Just printing some text';
- my @arr = ('array item 1', 'array item 2');
- my %hash = (
- 'key1' => 'value1',
- 'key2' => 'value2',
- );
- say '---- String ----';
- say $test;
- say '---- Array ----';
- say @arr;
- say '---- Hash ----';
- say %hash;
- say '---- Hash (fixed) ----';
- say \%hash;
|