scratchpad
davis
[tom_rpr]:
<code>
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
my %groups = (
group1 => { 'http://foo.com/' => {},
'http://bar.com/' => {},
'http://baz.com/' => {}, },
group2 => { 'http://abc.com/' => {},
'http://def.com/' => {},
'http://ghi.com/' => {}, },
);
foreach my $group (sort keys %groups) {
my $group_ref = $groups{$group};
foreach my $url (sort keys %$group_ref) {
my $url_ref = $group_ref->{$url};
# do stuff
$url_ref->{result} = 200;
}
}
print Dumper(\%groups);
</code>
woofle:
[http://www.nationmaster.com/encyclopedia/Formulation-of-Maxwell's-equations-in-special-relativity]
[drwoobie]:
<code>
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
my $username = param('username'); # info from login.html
my $cgi = CGI->new; # create CGI object
# try and connect to DB
my $dbh = DBI->connect("dbi:mysql:database", "username", "password", { RaiseError => 1, AutoCommit => 1 }) or
die "Can't connect: $DBI::errstr");
my $select_sth = $dbh->prepare("SELECT loc FROM users WHERE name = ?");
if($cgi->param("action") eq "update") {
my $update_sth = $dbh->prepare("UPDATE users SET loc=? WHERE name=?");
my $new_location = $cgi->param("newloc");
$update_sth->execute($new_location, $username);
}
# Pull player location from db
$select_sth->execute($username);
# Put player loc into variable based on previous pull
my $location = $select_sth->fetchrow_array;
print $cgi->header,
$cgi->start_html( -title => 'Begin: Eden v2');
print "You are $username and are located at $location";
print $cgi->start_form();
print $cgi->textfield(-name => 'newloc', -size => '4', -value=>$location, -force=>1);
print $cgi->hidden('username','username');
print $cgi->hidden(-name=>"action", -value=>"update");
print $cgi->submit(-value => 'name', -alias => 'username');
print $cgi->endform;
print $cgi->end_html;
$dbh->disconnect();
exit;
</code>
I can't decide if this is a perversion or not...
<code>
system("true") == 0
and print "true succeeded\n"
or warn "true didn't succeed\n";
system("false") == 0
and print "false succeeded\n"
or warn "false didn't succeed\n";
</code>
<code>
use warnings;
use strict;
use Benchmark qw(:all);
my @arr;
push @arr, int(rand(10000)) for (1 .. 1000000);
my($min, $max) = ($arr[0], $arr[0]);
cmpthese(100, {
no_break => sub {
for (@arr) {
$min = $_ if $_ < $min;
$max = $_ if $_ > $max;
}
},
break => sub {
for (@arr) {
if($_ < $min) {
$min = $_;
next;
}
$max = $_ if $_ > $max;
}
}
});
</code>