If you're stuck with pre-5.10, then the solutions provided above will not fully replicate the say
function. For example
sub say { print @_, "\n"; }
Will not work with invocations such as
say for @arr;
or
for (@arr) {
say;
}
... because the above function does not act on the implicit global $_
like print
and the real say
function.
To more closely replicate the perl 5.10+ say
you want this function
sub say {
if (@_) { print @_, "\n"; }
else { print $_, "\n"; }
}
Which now acts like this
my @arr = qw( alpha beta gamma );
say @arr;
# OUTPUT
# alphabetagamma
#
say for @arr;
# OUTPUT
# alpha
# beta
# gamma
#
The say
builtin in perl6 behaves a little differently. Invoking it with say @arr
or @arr.say
will not just concatenate the array items, but instead prints them separated with the list separator. To replicate this in perl5 you would do this
sub say {
if (@_) { print join($", @_) . "\n"; }
else { print $_ . "\n"; }
}
$"
is the global list separator variable, or if you're using English.pm
then is is $LIST_SEPARATOR
It will now act more like perl6, like so
say @arr;
# OUTPUT
# alpha beta gamma
#