Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Perl 6, arrays, hashes, subroutines & basic file IO

by tomazos (Deacon)
on Nov 01, 2006 at 02:06 UTC ( #581607=snippet: print w/replies, xml ) Need Help??
Description: Here is a small collection of Perl 6 code snippets lifted and mangled from pugs/examples/cookbook


my @a = <94 13 97 95>;

for @a -> $e {
    say $e;


my %hash = (
    'one'   => 'un',
    'two'   => 'deux',
    'three' => 'trois'

for %hash.keys -> $key {
    say "$key => %hash{$key}";

for %hash.kv -> $key, $value {
    say "The word '$key' is '$value' in French.";


sub perl5 {
    my ($x) = @_;
    say $x;


sub parameters ($foo) { say $foo }
parameters('some parameter');
parameters 'some parameter';
parameters foo => 'some parameter';
parameters :foo('some parameter');

sub whole (@names, %flags) {
    for @names -> $name { 
        say $name;

    for (%flags.kv) -> $key, $value {
        say "$key => $value";

my @stuff = ('array', 'elements');
my %flags = (hash => 'elements', are => 'pairs');

whole(@stuff, %flags);

sub optional ($required, $optional?) {
    my $second_arg = $optional // 'Told you it was optional!';
    say $required;
    say $second_arg;

optional('this', 'that');

sub named_params ($first, :$second, :$third) {
    say $first, $second, $third;

named_params(1, second => 2, third => 3);

sub transport ($planet, *@names) {
    say "Transporting to $planet:";
    for @names -> $name {
        say "\t$name";

transport('Magrathea', 'Arthur', 'Ford', 'Ovid');

sub typed (Int $val) {
    say $val + 3;



while $_ = =<> {


my $input = open("myfile.txt", :r) err die $!;

my $line = $input.readline;



my $output = open("out.txt", :w) err die $!;


Replies are listed 'Best First'.
Re: Perl 6, arrays, hashes, subroutines & basic file IO
by lidden (Curate) on Nov 01, 2006 at 15:19 UTC
    print; to print $_ is now spelled .print; notice the dot.
      print; to print $_ is now spelled .print; notice the dot.

      And also, with reference to the point you're actually referring to, for is now good for looping over a handle too, thanks to lazy evaluation, thus:

      # LOOP OVER STDIN for =<> { .print; }
Re: Perl 6, arrays, hashes, subroutines & basic file IO
by zentara (Archbishop) on Nov 01, 2006 at 12:52 UTC
    2 beginner questions on Perl6.

    1. Where is the 'my' in those variable usages?

    2. Why did they switch from 'print' to 'say'? It's shorter?

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      1. The only 'variables' I see without my in those snippets are formal parameter names, which are declarative only?
      2. say $var; is equivalent to print $var . $/;

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
      Where is the 'my' in those variable usages?

      As BrowserUk wrote, I don't see any missing. While we're here I'll take a chance to remind that differently from Perl 5, Perl 6 has strict and warnings turned on by default except in particular situations, e.g. in oneliners, where it's most often better otherwise.

      Why did they switch from 'print' to 'say'? It's shorter?

      Err, well, yes: easy things should be easy. And Perl has been somehow missing a writeln/println statement for quite a long time. But as also pointed out by BrowserUk, they did not "switch": you will have both print and say. The interesting thing to note here is that iirc in conjunction with the use of the latter autochomp actions are often taken, although it's not entirely clear to me how and when (I know the answer is out there I'm just too lazy ATM) - but it seems that $_ is not chomped by default:

      pugs> say .chars for <foo bar baz>; 3 3 3 undef pugs> say .chars for =<>; foo 4 bar 4 baz 4
Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: snippet [id://581607]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2023-12-11 13:13 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (41 votes). Check out past polls.