Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Lady_Aleena's scratchpad

by Lady_Aleena (Deacon)
on Sep 07, 2007 at 20:54 UTC ( #637756=scratchpad: print w/ replies, xml ) Need Help??

To display my pages, I use a subroutine called html (gist) as my main template and, most of the time, a subroutine called story which munges __DATA__. story has two named optional parameters doc magic and line magic. Here are several samples of my pages.

/Miscellany/ does not use story.

#!/usr/bin/perl use strict; use warnings FATAL => qw( all ); use CGI::Carp qw(fatalsToBrowser); use lib '../files/lib'; use Base::HTML qw(html); use Base::HTML::Element qw(section list); use Base::Data qw(data_file alpha_array); use Base::Menu qw(alpha_menu); use Util::Convert qw(searchify); use Util::Columns; use Util::ExternalLinks; open(my $gems_file, '<', data_file) || die "Can't open ".data_file.". +Stopped at $!"; my @gems = <$gems_file>; chomp @gems; my %alpha_gems = alpha_array(\@gems); for my $gems (keys %alpha_gems) { my @list = @{$alpha_gems{$gems}}; $alpha_gems{$gems} = undef; @{$alpha_gems{$gems}} = map { "$_ ".external_links([['Wikipedia',$_] +,['Google',searchify($_)]]) } sort @list; } html( code => sub { for my $alpha (sort keys %alpha_gems) { my $class = get_columns(3, scalar @{$alpha_gems{$alpha}}); section(3, sub { list(5, 'u', $alpha_gems{$alpha}, { 'class' => $class }); alpha_menu(4,\%alpha_gems); }, { 'heading' => [2, $alpha, { 'id'=> "section_$alpha" }] }); } });

/Writing/ uses story but has no line magic or doc magic.

#!/usr/bin/perl use strict; use warnings FATAL => qw( all ); use CGI::Carp qw(fatalsToBrowser); use lib '../files/lib'; use Base::HTML qw(html story); html( code => sub { story(*DATA) }); __DATA__ It was Christmas Eve, and Betty was ready for it. Her house was comple +tely decorated with lights inside and out. The tree sparkled with the + ornaments and tinsel. She had a few bags packed with gifts she was t +aking to her office to leave for her work friends. She had her Christ +mas music collection playing from her computer's speakers. She checke +d the caroling schedule and noticed that she didn't have a lot of tim +e to get the gifts distributed at the office. She took the bags out to her car along with a couple of canteens of wa +rm apple cider. It wasn't that cold out that Christmas Eve night; but + with the weather always waiting until after New Years to get nasty, +she was used to not having white Christmases. It wasn't that late in +the evening, but the roads were unusually empty. She saw only a few o +ther cars on her way back into work. "Maybe this year they got all of their shopping done early or online." + she said out loud to herself. She was able to park in a visitor's spot when she got to her building +because the office was closed for the holidays. She hummed the carols + that she would be singing that night with her church choir in town w +hile swiping her access card to get in the front door. When she was i +nside, she couldn't help but break out into actual song. She stopped +for a moment when she saw that the security desk wasn't manned. She s +ighed somewhat sadly when she remembered a memo going out to the empl +oyees that there would be layoffs, but those weren't going to happen +until after the new year. She looked inside her bags for an untagged +generic gift she could leave for the officers that had to work that n +ight. She placed the two little green and red boxes on the security d +esk and pushed the button on the elevator. She got to her office in a few minutes. It was a little chilly in the +building because the company usually turned all the thermostats back +to 50 degrees before the holidays to save on the electric bills. This + year it seemed that they may have turned the heat off completely. Sh +e then realized that the lights had been turned off in the parking lo +t too. She shook off these off occurrences as she distributed little +token gifts to all the cubicles in the room. Mostly she bought pens a +nd post-its, but she always added her own little flourish to the gift +s so that the recipients would always get a little smile whenever the +y used the mundane items she bought them. Taking as little time as po +ssible, she got back to the elevator and through the still empty lobb +y. She looked at the monitors again. The guards must be doing rounds +where there were no cameras she thought.

You can read the rest of the story here.

/Miscellany/ uses story which has some doc magic.

#!/usr/bin/perl use strict; use warnings FATAL => qw( all ); use CGI::Carp qw(fatalsToBrowser); use lib '../files/lib'; use Base::HTML qw(html story); use Base::HTML::Element qw(anchor list); use Base::Data qw(get_hash); my %BBSs = get_hash( 'file' => ['Miscellany', 'BBSs.txt'], 'headings' +=> [qw(name domain ip site)] ); my @items; for my $BBS (sort { $a->{name} cmp $b->{name} } values %BBSs) { my $name = $BBS->{name}; my $link = $BBS->{site}; my $BBS_head = $link ? anchor($name, { href => "http://$link" }) : $ +name; my @telnet_links; for my $telnet_link ('domain','ip') { my $link = $BBS->{$telnet_link}; push @telnet_links, anchor($link, { href => "telnet://$link" }) if + $link; } push @items, qq{$BBS_head <small>(}.join(', ',@telnet_links).q{)</sm +all>}; } my $doc_magic = { 'bbss' => sub { list(4,'u',\@items, { class => 'two' + }) } }; html( code => sub { story(*DATA, { 'doc magic' => $doc_magic }) }); __DATA__ This is a list of BBSs which I used to frequent or just know about. I +am not sure any still exist. My accounts on those I frequented have p +robably lapsed. & bbss

/Role_playing/Monsters/ uses story with a little line magic.

#!/usr/bin/perl use strict; use warnings FATAL => qw( all ); use CGI::Carp qw(fatalsToBrowser); use Lingua::EN::Inflect qw(PL_N); use lib '../../files/lib'; use Base::HTML qw(html story); use Util::Convert qw(searchify); my $line_magic; for (qw(twarg throglin tralg trobold gobpry zarden), 'dark centaur') { my $search = searchify(ucfirst $_); my $text = PL_N($_); $line_magic->{$text} = qq(A<$text|href="Monsters/$s +earch">); } html( code => sub { story(*DATA, { 'line magic' => $line_magic }) }); __DATA__ A high priest of Miskel, the evil godess of Triador, thought it would +please his Mistress and gain favor within his order, and a dark conju +rer were experimenting with the merging of different species. They di +d not expect their test subjects to revolt, but the ^twargs^, ^throgl +ins^, ^tralgs^, ^trobolds^, ^gobpries^, and ^zardens^ did. Only the ^ +dark centaurs^ stayed loyal to their masters. The twargs took the bru +nt of the attacks from the priest, conjuer, and their apprentices. Th +e tralgs, throglins, and trobolds fought the dark centaurs. The zarde +ns and gobpries either fought the non-magical henchmen or freed the p +eople used in the experiments. When the rebillion was over the priest's and conjurer's bodies could n +ot be found. Some think the gobpries flew off with them and either dr +opped or ate them. No one has ever found them. The dark centaurs fled + when the battle turned against them and there were only three studs +and five mares left. The twargs gained the respect of the other new races for their intelli +gence and wisdom which won the day. A throglin tried to take advantag +e of the situation to gain control of the throglins, tralgs, and trob +olds. The temprorary twarg leader of the battle convinced him to give + up the notion before they began fighting among themselves. Over the +days after the battle, only the zardens decided to stay together to f +ind a new home. Most of the other new races scattered in small bands. + Each race thrives in several communities spread across the four cont +inents leaving each other alone with the exception of rare attacks. They all left the city-sized caves where they were imprisoned with a d +eep resentment for the clergy of Miskel, but not Miskel herself, and +magic users in general. The twargs and zardans have great animosity t +owards the Church of Miskel. Even the dark centraurs who fought with +the priests and clerics keep themselves aloof from them. All of the n +ew races hate the dark centaurs to the point where the dark centaurs +are the species enemy to them all. The zardans are the most successful of all of the races. Since they ar +e part human, they can reproduce with humans. They rule Zilarb, the s +outhernmost nation in the Acrema Empire, and some southern parts of A +bru, the eastern island nation in The Acrama Empire. STRONG<Note:> Triador is a campaign world I began creating and may one + day put something of it up. Until then, you can use any evil god or +goddess in place of Miskel.

/Collections/ uses story and has doc magic and line magic.

#!/usr/bin/perl use strict; use warnings FATAL => qw( all ); use CGI::Carp qw(fatalsToBrowser); use lib '../files/lib'; use Base::HTML qw(html story); use Base::HTML::Element qw(list); use Base::LineMagic qw($line_magic); use People qw(get_people); my $doc_magic = { 'authors' => sub { list(3, 'u', [get_people('Authors.txt')], { 'clas +s' => 'three' }) }, }; html( code => sub { story(*DATA, { 'doc magic' => $doc_magic, 'line ma +gic' => $line_magic }) }); __DATA__ This is my fiction collection of SPAN<hardcovers|^hardcovers^>, SPAN<t +rade paperbacks|^trades^>, and SPAN<mass market paperbacks|^massmarke +ts^>. 2 General fiction * I<Big Trouble|^hardcover^> by Dave Barry * I<The House of Ulloa|^hardcover^> by Emilia Pardo Bazan (Trans. by R +oser Caminals-Heath) * I<The Da Vinci Code|^trade^> by Dan Brown * I<Through the Looking Glass|^trade^> and I<Alice's Adventures in Won +derland|^trade^> by Lewis Carroll * I<Sophie's World|^massmarket^> by Jostein Gaarder * I<The Walking Man|^trade^> by William Heath * I<A Seperate Peace|^massmarket^> by John Knowles * I<The Key to the Kingdom|^hardcover^> by Tony Meeuwissen * I<Paradise Lost and Paradise Regained|^massmarket^> by John Milton * I<Literature|^hardcover^> by James H. Pickering and Jeffery D. Hoepe +r 2 Authors I like & authors

The list is far longer than above, and you can see it here.


my $noon = 12:00; my %dayparts = ( 'morning' => { 'begin' => $dawn, 'end' => $noon}, 'afternoon' => { 'begin' => $noon, 'end' => $dusk - 1}, 'evening' => { 'begin' => $dusk - 1, 'end' => $dusk + 1}, 'night' => { 'begin' => $dusk + 1, 'end' => $dawn} );

Notes to self

  • Do not print Dumper when trying to display a script in a browser without using <pre> tags.
  • Do print Dumper when something goes wrong. The data structure is usually the problem.
  • If one changes a variable, check the whole script for that variable to make sure that the results will be as expected.
  • rename will not create directories, be sure to use mkdir in conjunction with rename if the files are also being moved to a new directory.

Notes from others

  • bart: LA it's often considered good practice to increment the variable in a continue block, to prevent running out of sync when using next.
  • Intrepid: btw, i wish i could explain to a lot of people i know, how efficacious Lady_Aleena's explained strategy can be (to use coding and thinking about coding to master a disquieted state of mind)
  • ambrus: In general, use magic only when normal solutions aren't enough.
  • GrandFather: each time through its loop while asks itself "Can I loop this time? Can I? Can I Can I? Please?" and if the answer is no (false statement) while gets grumpy and stops
  • GrandFather: First make it work (using the easiest to maintain code you can) then, only if you need to, make it faster. (from: Re^5: I have a list expanding when I don't want it to)

Things I should get a grip on

  • objects
  • SQL
  • version control
  • testing
Log In?

What's my password?
Create A New User
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (11)
As of 2015-03-05 09:20 GMT
Find Nodes?
    Voting Booth?

    When putting a smiley right before a closing parenthesis, do you:

    Results (138 votes), past polls