Camelia
Prev: Subroutine with arbitrary number of parameters

Passing arrays and hashes

Note! This site is about Perl 6.
If you are looking for a solution for the current production version of Perl 5, please check out the Perl 5 tutorial.

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.)

tutorial/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

tutorial/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:

tutorial/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).


The Perl 6 Tricks and Treats newsletter has been around for a while. If you are interested to get special notification when there is new content on this site, it is the best way to keep track:
Email:
Full name:
This is a newsletter temporarily running on my personal site (szabgab.com) using Mailman, till I implement an alternative system in Perl 6.
Gabor Szabo
Written by Gabor Szabo

Published on 2012-01-01


Prev: Subroutine with arbitrary number of parameters

Comments

In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation.
comments powered by Disqus
Suggest a change
Elapsed time: 2.9450625

Perl 6 Tricks and Treats newsletter

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