Golf challenge: print the numbers from one to a hundred in English (each on its own line). (Yes, I know I could save a few strokes with say if I had Perl 5.10.)
$,=$/;print@a=qw[one two three four five six seven eight nine],qw[ten eleven twelve],(map{s/ree/ir/;s/ve$/f/;s/t$//;$_.'teen'}@b=@a[2..8]),(map{s/u//;$t=$_.'ty',map{"$t-$_"}@a}$w='twen',@b),'one hundred'
A quine that tries to follow some of the guidelines in
Perl Best Practices:
my $generator = <<'END_GENERATOR';
my $self = <<"END_SELF";
my \$generator = <<'END_GENERATOR';
${generator}END_GENERATOR
eval \$generator;
END_SELF
print $self;
END_GENERATOR
eval $generator;
DBI snippet: read tab-separated data and insert it into a database table.
use DBI;
my ($connect_string, $table, @columns) = @_? @_: @ARGV;
my $dbh = DBI->connect(
"dbi:ODBC:$connect_string",
undef,
undef,
{ RaiseError => 1 },
);
my @placeholders = map {'?'} @columns;
my $insert_statement = do {
local $" = "\n, ";
"Insert into $table (@columns) values (@placeholders)";
};
my $sth = $dbh->prepare($insert_statement);
while (<>) {
chomp;
my @values = split /\t/;
$sth->execute(@values);
}
FindBin and use lib for an environment I work in
my $root;
BEGIN {
use FindBin;
($root) = $FindBin::Bin =~ m{ (.* racine) [\\/] }imsx;
}
use lib "$root/Jobs/Perl/lib";
|