Scalar::MultiValue - Create a SCALAR with multiple values.
This module create a SCALAR with multiple values, where this values can be randomic or can change by a defined period.
With a period of 2:
my $s = new Scalar::MultiValue( [qw(a b c d)] , 2 ) ; for(0..8) { print "$s\n" ; }
Output:
a a b b c c d d
With randomic values:
my $s = new Scalar::MultiValue( [qw(a b c d)] , '*' ) ; for(0..8) { print "$s\n" ; }
c d c b a d c c
The arguments of new are a LIST and the PERIOD (optional):
Can be a ARRAYREF or a string that will be splited by /\s/, like on qw():
## this is the same my $s = new Scalar::MultiValue( 'a b c d' ) ; ## of that: my $s = new Scalar::MultiValue( [qw(a b c d)] ) ;
The PERIOD can be a integer value, that will define how many times a value will be repeated before change to the next value. PERIOD also can be '*', that will change randomically the values.
You can use the scalar as a ARRAYREF and set it's values
my $s = new Scalar::MultiValue( 'a b c d' ) ;
Redefining a single value:
$$s[0] = 'A' ;
Redefining all the values:
@$s = qw(w x y z) ;
Return the last value (without change the internal counter).
Reset the internal counter for the PERIOD.
Return the period or define it when VAL is defined.
From version 0.03 you also can access the values of the methods above as an attributes (HASH key):
my $colors = new Scalar::MultiValue('#CCCCCC #999999') ; print "<font color='$colors'>Main Color</font>\n" ; print "<font color='$colors->{last}'>Previous Color</font>\n" ;
A common example of use for this module is for multiple colors on a table:
use Scalar::MultiValue ; my $colors = new Scalar::MultiValue('#CCCCCC #999999') ; my @users = qw(a b c d) ; print "<table>\n" ; foreach my $users_i ( @users ) { print "<tr><td bgcolor='$colors'>$users_i</td></tr>\n" ; } print "</table>\n" ;
<table> <tr><td bgcolor='#CCCCCC'>a</td></tr> <tr><td bgcolor='#999999'>b</td></tr> <tr><td bgcolor='#CCCCCC'>c</td></tr> <tr><td bgcolor='#999999'>d</td></tr> </table>
Scalar::Util.
Graciliano M. P. <gmpassos@cpan.org>
I will appreciate any type of feedback (include your opinions and/or suggestions). ;-P
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Scalar::MultiValue, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Scalar::MultiValue
CPAN shell
perl -MCPAN -e shell install Scalar::MultiValue
For more information on module installation, please visit the detailed CPAN module installation guide.