Help-Site Computer Manuals
Software
Hardware
Programming
Networking
  Algorithms & Data Structures   Programming Languages   Revision Control
  Protocols
  Cameras   Computers   Displays   Keyboards & Mice   Motherboards   Networking   Printers & Scanners   Storage
  Windows   Linux & Unix   Mac

Perl6::Binding
implement Perl6 aliasing features

Perl6::Binding - implement Perl6 aliasing features


NAME

Perl6::Binding - implement Perl6 aliasing features


SYNOPSIS


        use Perl6::Binding;

        my ($foo, @bar, %baz) := @hash{qw/foo bar baz/};

        my ($foo, @bar, %baz) := *%hash;

        my ($foo, @bar, %baz) := *@array;

        my @array1 := @array2;


DESCRIPTION

This module creates lexical aliases to items that can be either lexical or dynamic using the := operator. The left side of := is a variable or a list of variable names in parentheses. The right side is a list of items to which the items on the left should should refer. Each item on the left side is made an alias to the corresponding item on the right.

What's an Alias?

An alias is a way of making the same value have more than one way to get at it. For example, after the statement:


        my $foo := $array[2];

anyplace you refer to $foo, you are actually referring to $array[2]. Changing either one is the same as changing the other. If you take a reference to each of them, you'll discover that the references are identical.

The example above may not look that useful, but something like this could be:


        my %hash := %{$parameter->{index}->{option}};

Now you can type $hash{foo} instead of < $parameter-{index}->{option}->{foo} >>. Not only does this save typing, but it should execute slighly faster as well.

Perl automatically creates aliases to the items in the @_ array when a function is called, and to the variable in a foreach statement. So, after a statement like


        my ($foo, @bar, %baz) := *@_;

the items are aliases to the actual parameters passed to the function. Changing the value of $foo changes the value of the item that was passed as the first parameter.

The * on the right side of := indicates that the item it prefixes is to be flattened. That is, the contents are considered as if they had been added to the list explicitly. The following two lines are equivalent, except that the second one requires less typing:


        my ($foo, @bar, %baz) := ($array[0], @{$array[1]}, %{$array[2]});

        my ($foo, @bar, %bax) := *@array;

The * can appear before any number of items on the right side, and before either arrays or hashes. However, using it on a hash causes everything after it on the right side to be ignored, and selects the items from the hash that are to be aliased from the names of the variables on the left side. The following two statements have identical effects:


        my ($foo, @bar, %baz) := @hash{qw/foo bar baz/};

        my ($foo, @bar, %baz) := *%hash;

You can also do something like this:


        my ($name, *@parameters) := *@_;

This says that $name is to be an alias to $_[0], while the rest of the contents of @_ are copied to @parameters, which becomes a ``real'' array rather than an alias. Changing the items in @parameters does not affect the values passed to the function. The * on the left side says ``throw everything else into this variable.'' It may only be used on the last item (or rather, anything after it in the list will neither become an alias to anything nor have a value assigned to it).

If the variable prefixed by * is a scalar, it receives the count of the remaining items rather than any of the items themselves.

The type of the left item and the type of the right item must match. The following statements are invalid:


        my @foo := %bar;

        my $baz := @foobar;

This module works both at compile time (via a source filter) and at runtime.


NOTES

  • It's possible that the source filter might find something that looks like the statements it handles in odd locations, such as within a string. If this happens, use no Perl6::Binding to turn off the filter where necessary. Don't forget to turn it back on afterwards!

  • This is currently alpha software. It seems to work, but I am sure there are odd bugs lurking in the woodwork. Please let me know if you find them.

  • Version 0.6 fixes a long-standing problem in that bindings in recursive subroutines did not work. Now they do.

  • Version 0.601 is an update to 0.6 that puts the dependencies back into the Makefile.PL.


REQUIRED MODULES

Filter::Util::Call

Text::Balanced

PadWalker


BUGS

Under Perl 5.8.x, it is not possible to create aliases at the root level of the program due to a problem in PadWalker 0.09 and 0.10 (see the README for PadWalker). Aliases created in subroutines continue to work, however.


ACKNOWLEDGEMENTS

Some code was taken from Devel::LexAlias and Devel::Caller, both by Richard Clamp.

The name Perl6::Binding was suggested by Benjamin Goldberg.


AUTHOR

Kevin Michael Vail <kevin@vaildc.net>


COPYRIGHT AND LICENSE

Copyright 2003 by Kevin Michael Vail

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

Programminig
Wy
Wy
yW
Wy
Programming
Wy
Wy
Wy
Wy