Perl: Difference between revisions
Jump to navigation
Jump to search
(→Script Examples: Using Anonymous Hash References) |
|||
Line 1: | Line 1: | ||
== Script Examples == |
|||
=== Using Anonymous Hash References === |
|||
<source lang=perl> |
|||
#!/usr/bin/perl |
|||
use strict; |
|||
my @myarray; |
|||
foreach my $iter ( 1..10 ) |
|||
{ |
|||
my $value1 = "value1_".$iter; |
|||
my $value2 = "value2_".$iter; |
|||
my $value3 = "value3_".$iter; |
|||
print "Creating our \$hashref... "; |
|||
# Construct { key1 => value1, key2 => value2.... } creates a REFERENCE to an anonymous hash. |
|||
# Since reference are SCALAR, we assign it to a scalar variable |
|||
my $hashref = { index1 => $value1, index2 => $value2, index3 => $value3 }; |
|||
print "Done.\n", |
|||
" \$hashref: ",$hashref,"\n"; |
|||
print " content: ",$$hashref{'index1'},",",$$hashref{'index2'},",",$$hashref{'index2'},"\n"; |
|||
print "Adding \$hashref to our array... "; |
|||
push( @myarray, $hashref ); |
|||
print "Done. There are currently ", scalar(@myarray), " elements in \@myarray.\n"; |
|||
print "Accessing last element of our array..."; |
|||
print " content: @myarray[$#myarray], ${@myarray[$#myarray]}{'index1'} our better yet @myarray[$#myarray]->{'index2'}\n"; |
|||
} |
|||
print "\n\nNow we will traverse our array again...\n"; |
|||
foreach ( @myarray ) |
|||
{ |
|||
print "$_ containing ", |
|||
"index1 => $$_{'index1'},", |
|||
"index2 => $$_{'index2'},", |
|||
"index3 => $$_{'index3'}\n"; |
|||
print "... or using -> operator: ", |
|||
"index1 => $_->{'index1'},", |
|||
"index2 => $_->{'index2'},", |
|||
"index3 => $_->{'index3'}\n"; |
|||
} |
|||
</source> |
|||
== CPAN - Perl Packages == |
== CPAN - Perl Packages == |
||
Revision as of 08:50, 19 January 2009
Script Examples
Using Anonymous Hash References
#!/usr/bin/perl
use strict;
my @myarray;
foreach my $iter ( 1..10 )
{
my $value1 = "value1_".$iter;
my $value2 = "value2_".$iter;
my $value3 = "value3_".$iter;
print "Creating our \$hashref... ";
# Construct { key1 => value1, key2 => value2.... } creates a REFERENCE to an anonymous hash.
# Since reference are SCALAR, we assign it to a scalar variable
my $hashref = { index1 => $value1, index2 => $value2, index3 => $value3 };
print "Done.\n",
" \$hashref: ",$hashref,"\n";
print " content: ",$$hashref{'index1'},",",$$hashref{'index2'},",",$$hashref{'index2'},"\n";
print "Adding \$hashref to our array... ";
push( @myarray, $hashref );
print "Done. There are currently ", scalar(@myarray), " elements in \@myarray.\n";
print "Accessing last element of our array...";
print " content: @myarray[$#myarray], ${@myarray[$#myarray]}{'index1'} our better yet @myarray[$#myarray]->{'index2'}\n";
}
print "\n\nNow we will traverse our array again...\n";
foreach ( @myarray )
{
print "$_ containing ",
"index1 => $$_{'index1'},",
"index2 => $$_{'index2'},",
"index3 => $$_{'index3'}\n";
print "... or using -> operator: ",
"index1 => $_->{'index1'},",
"index2 => $_->{'index2'},",
"index3 => $_->{'index3'}\n";
}
CPAN - Perl Packages
First time launch:
$ cpan # ... OR ...
$ perl -MCPAN -e shell # --> yes auto config
To adapt config related to proxy:
cpan> o config init /proxy/ # (to enter an empty string, simply enter 1 space char as a value)
cpan> o conf commit
To install a Perl package (eg. here package GetOpt::Long):
$ cpan
cpan>install GetOpt::Long
Editing the configruation:
cpan> o conf init # Reset the configuration
cpan> o conf http_proxy http://proxy:8080/ # Edit a given variable (eg. here changing proxy settings):
cpan> o conf commit # To commit changes in the configuration
cpan> o # to get o options
cpan> o conf # To get o conf option
To edit CPAN database url:
cpan> o conf /urllist/
cpan> o conf init /urllist/
cpan> o conf urllist shift
cpan> o conf urllist unshift ftp://my.new.site/
cpan> o conf commit
To update CPAN itself:
cpan> install Bundle::CPAN
cpan> reload cpan