Camelia
Prev: Subroutine with arbitrary number of parameters

Passing arrays and hashes

In Perl 5 we had to learn about references in order to be able to pass more than one array or hash to a subroutine. That also meant the subroutine always has full access to the original data structure. Effectively a call-by-reference thing. So the subroutine could change the passed parameter by accident. Not really clean coding.

In Perl 6 there are no such references and there is no need for them. If you'd like to accept a real array or hash as a parameter for a subroutine you can declare that in the signature of the subroutine.

For example I have a script that generates html pages from templates and I have some code like this: (Mine was using the HTML::Template from the November wiki project http://www.november-wiki.org/ but that's not relevant now so I am just printing out the parameters.)

examples/subroutines/array_hash_arguments.p6
#!/usr/bin/env perl6
use v6;

sub process_template($input, $output, %params) {
    say "open $input";
    say "replace {%params.perl}";
    say "save $output";
}

my %data = (
    fname => "Foo",
    lname => "Bar",
);

process_template("index.tmpl", "index.html", %data);

the output

examples/subroutines/array_hash_arguments.p6.out
open index.tmpl
replace {"lname" => "Bar", "fname" => "Foo"}
save index.html

Now I know this example does not show the real power of having several complex data structures in the parameter list. I could have written:

 sub xyz($input, @value, %data) { }

and then pass a scalar, an array and a hash. I just don't have a good example for that yet.

Anyway the above example had a bit of data multiplication and the name of the output file could have been generated from the name of the template. It is the same name just with different extension.

So I could write the code like this, passing only the name of the template and generating the name of the output file from that:

examples/subroutines/process_template.p6.out
open index.tmpl
replace {"lname" => "Bar", "fname" => "Foo"}
save index.html

In this code ~ is the concatenation operator (replacing the . from Perl 5).


Prev: Subroutine with arbitrary number of parameters

Table of Contents

Comments

In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation.
comments powered by Disqus

Perl 6 Tricks and Treats newsletter

Register to the free newsletter now, and get updates and news.
Email:
Name: