Here's one way (use $all_msgs instead of @all_msgs):
use warnings;
use strict;
my @all_files = 5..7;
errors(
all_msgs => \@all_files,
msg_subject => 'Error files',
mail_msg => 'Listed files for processing:',
);
sub errors {
my (%args) = @_;
my $all_msgs = $args{all_msgs} || '';
my $msg_subject = $args{msg_subject} || '';
my $mail_msg = $args{mail_msg} || '';
foreach my $msgs (@{ $all_msgs }) {
print "$msgs\n";
}
}
__END__
5
6
7
| [reply] [d/l] |
Thank you! I spaced out on getting it with "$" and not with "@".
Thats the way:
my $all_msgs = $args{all_msgs} || '';
and processing it:
foreach my $msgs (@{ $all_msgs }) {
...
Problem solved, thanks again!
| [reply] [d/l] [select] |
use strict; use warnings;
errors(
{
'all_msgs' => 'foo',
'msg_subject' => 'bar',
}
);
sub errors {
my $args = shift;
my $all = $$args{'all_msgs'} || '';
...
}
A few things to notice here, and they’re partly just stylistic:
- Perl lets you have a trailing-comma, as I do in my call to errors(), for the pure-and-simple reason that it's a bit easier to add new stuff after it as you revise the program. The hash as-shown contains two keys, not three.
- I enclose the hash-keys in quotes, vs. the so-called “barewords.”
- To me, the use of use strict; use warnings; is obligatory.
- In the snippet of code defining errors() itself, $$args{'foo'} is a shorthand for $args->{'foo'} that I happen to prefer.
In this example, the first (and only) argument to the subroutine is “a reference to a hash,” otherwise known simply as a “hashref.” Perl uses the notion of a “ref” quite extensively, allowing you to build up arbitrarily complex data structures.
| |