Skip to content

Commit

Permalink
[subs] flesh out/add some examples
Browse files Browse the repository at this point in the history
  • Loading branch information
moritz committed May 7, 2010
1 parent 8e36bf7 commit f7a5527
Showing 1 changed file with 43 additions and 11 deletions.
54 changes: 43 additions & 11 deletions src/subs-n-sigs.pod
Expand Up @@ -331,15 +331,34 @@ Argument names do not necessarily have to correspond exactly to parameter names

=end programlisting

=for author
Parameters can also have multiple names. If the users are both British and
Americans, one might write:

Does this imply that the binder will bind either color => 'Blue' or colour =>
'Bluue'? An example is I<very> useful, if so.
=begin programlisting

=end for
sub paint-rectangle(
:$x = 0,
:$y = 0,
:$width = 100,
:$height = 50,
:color(:colour($c))) {

# print a piece of SVG that reprents a rectangle
say qq[<rect x="$x" y="$y" width="$width" height="$height"
style="fill: $c" />]
}

# both calls work the same
paint-rectangle :color<Blue>;
paint-rectangle :colour<Blue>;

Parameters can also have multiple names. If the users are both British and
Americans, one might write C<:color(:colour($c))> or C<:color(:$colour))>.
# and of course you can still fill the other options
paint-rectangle :width(30), :height(10), :colour<Blue>;


=end programlisting

C<:color(:colour($c))> or C<:color(:$colour))>.

=head3 Alternative Named Argument Syntaxes

Expand Down Expand Up @@ -376,7 +395,17 @@ A named argument of the form C<:name> with no value has an implicit value of
C<Bool::True>. The negated form of this, C<:!name>, has an implicit value of
C<Bool::False>.

The following table lists other possible C<Pair> forms and their meanings:
If you use a variable to create a pair, you can reuse the variable name as the
key of the pair.

=begin programlisting

my $dinner = '9pm';
announce-dinner :$dinner; # same as dinner => $dinner;

=end programlisting

The following table lists possible C<Pair> forms and their meanings:

Shorthand Long form Description

Expand All @@ -390,13 +419,16 @@ The following table lists other possible C<Pair> forms and their meanings:
:%var var => %var Hash variable

You can use all of these forms in any context where you can use a C<Pair>
object.
object, for example when populating a hash:

=for author
=begin programlisting

This also needs an example.
# TODO: better example
my $black = 12;
my %color-popularities = :$black, :blue(8),
red => 18, :white<0>;

=end for
=end programlisting

Finally, to pass an existing C<Pair> object to a subroutine by position, not
name, either put it in parentheses (like C<(:$thing)>), or use the C<< => >>
Expand Down

0 comments on commit f7a5527

Please sign in to comment.