Hey Mongers! ps. Joel, file attached.
Garrett Casey
canetguy at home.com
Thu Jun 7 20:33:24 CDT 2001
Hey everyone...
I am sorry I haven't been able to attend many meetings lately. I have been dealing with some personal stuff.
I need someone to volunteer to meet me sometime before June 20th. I have some TPJ's that need to be presented to the group, along with a bunch of catalogs and pamphlets from O'reilly.
I will not be able to attend the June meeting. I will be in NJ planning for my new job as a Market Maker for Knight Trading. I will be moving to NJ in August or September :(
-Garrett
ps. Joel, I found the perl code file you requested on my old lap top. I have attached it to the email. Enjoy!
-------------- next part --------------
## Scalars
my $thingy = 100;
my $ref_thingy = \$thingy;
print $ref_thingy; #Prints similar to SCALAR(0x89230)
print ${ $ref_thingy }; #Prints 100;
${ $ref_thingy }++;
print $thingy; #Prints 101
my $ref_const = \100; #Take reference of constant.
${ $ref_const }++; #You get a runtime error. The value pointed to by $ref_const is read-only.
## Arrays
my @holder = qw(zero one two);
my $ref_holder = \@holder;
foreach ( @{ $ref_holder} ) { #Prints each element of @holder.
print $_, "\n";
}
print $ref_holder->[1], "\n"; #Prints 'one';
for my $i (0..$#{$ref_holder}) {
print $ref_holder->[$i], "\n"; #Prints each element of @holder.
}
my $ref_another = [ qw(three four five) ]; ##Anonymous Creation of Array Ref.
print join(', ', @{ $ref_another }), "\n"; ##Prints three, four, five
map push(@{ $ref_another }, $_), ('A'..'Z');
print scalar @{ $ref_another }, "\n"; ##Prints 29
sub big_array {
my @big_array = (0..10000); #Or more.
## Tons of awesome code ##
return \@big_array;
}
my $ref_big = big_array(); ## You are not copying a huge array (or hash).
print $ref_big->[9999], "\n"; ## Prints 9999
## Hashes
my %names = ( dog => 'Bowser', sister => 'Jane', brother => 'Bob');
print $names{dog}, "\n"; ## Prints Bowser
my $ref_names = \%names;
print $ref_names->{sister}, "\n"; ## Prints Jane
foreach my $name (keys ( %{ $ref_names } ) ) {
print "$name\n";
}
my $ref_more = $ref_names;
$ref_more->{sister} = 'Bonnie';
print $ref_names->{sister}, "\n"; ## Prints Bonnie
## Anonymous Hash
my $h_ref_age = { grandma => 81, dad => 50, sister => 17, brother => 21 };
print "Grandma is $h_ref_age->{grandma} years old!\n";
## Code
sub rand_char { return ('A'..'Z')[ int rand 26 ] };
my $ref_code = \&rand_char;
print $ref_code->(), "\n";
## Not the code below DOES NOT create a reference of the
## of the code, but instead, returns a reference to the
## last value returned by the function
my $ref_not_code = \rand_char();
print ${ $ref_not_code }, "\n"; ## Prints the last value return by the code.
my $ref_other_code = sub { print $_[0] x 5; }; ## Anonymous code ref;
$ref_other_code->('x'), "\n"; ## Prints xxxxx
## References of References
## Yes, you can take references of other references.
my $number = 100;
my $r_numb = \$number;
my $rr_num = \$r_numb;
print ${ ${ $rr_num } }, "\n"; ## Prints 100
## Notice that all the references are SCALARS and that SCALARS can be
## stored in arrays and hashes. So with references, you can create
## cool structures.
## More Arrays
my $aa = [ [0..10], [11..20], [21..30] ]; #Anonymous array reference of an array of anonymous arrays
print $aa->[1][8], "\n"; #Prints 19;
## Array of hash references.
my $ab = [ { apple => 'green', grape => 'purple' }, { cow => 'mooo', dog => 'arf' } ];
print $ab->[0]{apple}, "\n"; #Prints green.
my ($lname, $fname, $email) = qw(Sparky McDoodle sparky at spark.com);
my @guy_array = \($lname, $fname, $email); ## Array of references.
print ${ $guy_array[0] }, "\n"; ## Prints Sparky
## More hashes
## Hash of anonymous array. Wierd example.
my %thing = ( names => [ qw(Bob Harry Joe) ], ages => [ 9, 14, 16 ] );
for my $i (0..$#{$thing{names}}) {
print "$thing{names}->[$i] is $thing{ages}->[$i] years old.\n";
}
## Hash of hash.
my %class = (
Bob => { age => 9, mother => 'Betty', father => 'Frank' },
Harry => { age => 14, mother => 'Joan', father => 'Dean' },
Joe => { age => 16, mother => 'Sue', father => 'Fred' },
);
foreach (keys (%class) ) {
print "$_ is $class{$_}{age} years old.\n";
}
__END__
#Big structure.
#I created a little module to help with CGI fields.
#It is still a little rough, but you can see the kind
#of structure I pass to it.
my $field = new CGIField( {
text => 'Type of Property',
required => 1,
type => 'popup_menu',
values => [qw/select apartment condo vacation/],
name => '_property_type',
labels => {
select => '- Select the Type of Property -',
apartment => 'Apartment',
condo => 'Condo'
vacation => 'Vacation Rental',
},
errorsub => sub {
my $q = shift;
return "You did not select what type of property."
if ($q->param('_property_type') eq 'select');
return undef;
},
} );
#Basically, my CGIField object is a container for a CGI object definition
#that keeps its own error subroutine. The module also has some cool methods
#that just makes things easier.
#This is simply an example of a complex structure. Notice that CGIField is
#passed an Anonymous hash that contain some text, arrays, other hashes
#and even some code.
#Further Reading:
# O'Reilly's Programming Perl pages 243-275
# O'Reilly's Perl Cookbook 364-394
More information about the San-Diego-pm
mailing list