Perl

2 ways to get values out of a hash using loops.

Make the hash

[- %customers = (
"AT&T Corp","1.2.11.1",
"BellSouth Corp","1.2.16.1",
"British Telecommunications","1.2.19.1",
"BroadWing Inc","1.2.155",
"China Unicom","1.2.3"
) -]

Output using "foreach"

    [$ foreach $name (sort keys %customers) $]
  • [+ $name +]
  • [$ endforeach $]

Output using "do"

[- $total = scalar(keys %customers); -]
[- $i = 0 -]
    [$ do $] [- @k = keys %customers; @v = values %customers; -]
  • [+ $k[$i] +]
  • [- $i++ -] [$ until $i == $total $]

Code to alternate background colors in table rows.

<style type="text/css">
.odd {
  background: #ddd;
}
</style>

<table>
[* foreach $num (1 .. 10){ *]

[$ if ($num % 2) $]
<tr><td class="odd">[+ $num +] is odd</td></tr>
[$ else $]
<tr><td class="even">[+ $num +] is even</td></tr>
[$ endif $]

[* } *][# endforeach #]
</table>

Code to list files in a directory.

[-
  $dir = opendir(DIR, '[YOUR DIRECTORY PATH HERE]');
  @tmplist = readdir(DIR);
  @list = grep (!/^\./, @tmplist);
-]

<ul>
[$ foreach $file (sort @list) $]
  <li><a href="/drupal-4-7-2/[+ $file +]">[+ $file +]</a></li>
[$ endforeach $]
</ul>

Code to redirect a page to another URL.

[-
# mod_perl redirect
use Apache;
use Apache::Constants qw(REDIRECT);
# url goes here
$req_rec->header_out("Location" => "[PUT YOUR URL HERE]");
$req_rec->status(REDIRECT);
-]

I'm starting to copy the simple bits of embedded Perl code I've used over the past few years. As you may know, my company has plans to merge with (or be acquired by) Alacatel. One popular form of knowledge management is to mine your people's processes, methodologies and assets before you fire them reduce headcount. I've already been pretty good at capturing the necessary bits about processes/methodologies on our Wiki over the past few years. I do this in case, as we usually say, one of us gets by a bus or something. Morbid. So, anyway, they won't have to mine my head. But there are little bits of actual code all over the place that I haven't captured for myself.

So I'm going to start doing some retrospective knowledge management for myself to capture some of the methods I've used on presentation-layer server-side scripting with Embperl. These are mostly the little subroutines that I think might be reusable. I'm not sure I'll ever work in Embperl outside of this place. I'm not even sure there are really that many people interested in Embperl out there, but if you are, you'll be able to find the scripts in Resources section over the next few weeks.

For the non-perl stuff (e.g. IA/ID-related processes and methodologies, html and css snippets), I've been keeping notes here on the blog as well as in my wiki. Not sure if I'll publish any of that though because a lot of it requires explanation. The nice thing about the little code snippets is that they're for doing very small things in Perl and are pretty self-explanatory.

Simple subroutine for checking browser using HTML::Embperl. The version below detects if a modern browser is being used and assigns the $browser variable a value of "DOM" or "CRAP".

[# browser detection for search modules #]
[$ sub browserDetection $]
[-
  $browser = $ENV{HTTP_USER_AGENT};
  if ($browser =~ /MSIE/) { $browser_vers = 'IE' };
  if ($browser =~ /Gecko/) { $browser_vers = 'GECKO' };
  if (($browser_vers eq 'IE') ||
    ($browser_vers eq 'GECKO')) {
    $browser = 'DOM';
  } else {
    $browser = 'CRAP';
  }
-]
[$ endsub $]

This is an example showing how to handle HTML::EMBPERL email delivery via an HTML form taken from the EMBPERL site. You may want to add some security checking.

[-
use URI::Escape;
$MailTo = "$fdat{mailto_email}";
$myURI = $fdat{URI};
$name = $fdat{name};
$email = $fdat{email};
$msgtxt = $fdat{msgtxt};

@errors = () ;
if ( defined($fdat{check}) || defined($fdat{send}) )
{
push @errors, "Please enter your name" if (!$fdat{name});
push @errors, "Please enter your e-mail address" if (!$fdat{email});
push @errors, "Please enter friend's name" if (!$fdat{mailto_name});
push @errors, "Please enter friend's email address" if (!$fdat{mailto_email});
push @errors, "Please enter a message" if (!$fdat{msgtxt});
}
-]

[$if (defined($fdat{check}) and $#errors == -1)$]
[- delete $fdat{input} ; delete $fdat{check} ; delete $fdat{send} -]

You have entered the following data. Please confirm and click Send when done.
Your name[+$fdat{name}+]
Your email[+$fdat{email}+]
Friend's Name[+$fdat{mailto_name}+]
Friend's email[+$fdat{mailto_email}+]
Message[+ $msgtxt +]
 
[$hidden$] [# the hidden metacommand creates hidden fields for every entry in %fdat whis is not used by any other input tag. #]
[$elsif defined($fdat{send}) and $#errors == -1$] [- # SET SUBJECT HERE $subject = "Email from: $name"; # INTRODUCE MSG HERE $msg = "\n MESSAGE SENT TO YOU FROM $name ($email) \n\n"; # Open The Mail Program - change this to your path for sendmail $mailprog = '/usr/lib/sendmail'; open(MAIL,"|$mailprog -t"); print MAIL "To: $MailTo\n"; print MAIL "From: nobody\@yourdomain.com\n"; print MAIL "Subject: $subject\n"; print MAIL "\n ========================================================== \n"; print MAIL "$msg"; print MAIL "$msgtxt"; print MAIL "\n ========================================================== \n"; close (MAIL); -] Your message has been sent. [$else$] [- $optDisableTableScan = 0 -] [$if $#errors != -1 $] [$endif$]
[+$errors[$row]+]
[- $optDisableTableScan = 1 -]
Your information 
Your name
Your email address

Recipient's information 
Friend's name
Friend's email address
Message
[$endif$]

Use this script to display a randomized item from an array.

[-
# RANDOM TEXT GENARATOR
# you can put images in the array
# be sure to escape any quotes, slashes, etc. in the array values
@randtxt = (
"Thus heavenly hope is all serene, But earthly hope,
how bright soeíer, Still fluctuates oíer this changing
scene, As false and fleeting as ít is fair.",
"Hope tells a flattering tale, Delusive, vain, and
hollow. Ah! let not hope prevail, Lest disappointment
follow."
);
-]
[+ $randtxt[int rand(@randtxt)] +]

This article is a case study of the use of Perl and XML/RDF technologies to channel disparate sources of data into a semi-structured repository.

A bookmark manager (del.icio.us clone) available as Open Source Perl software. Rubric is used on the de.lirio.us site.