tag:blogger.com,1999:blog-117226582024-03-13T18:45:37.752-04:00Tinypig BlogTinypig BlogDavid M. Bradfordhttp://www.blogger.com/profile/04202175322890062001noreply@blogger.comBlogger37125tag:blogger.com,1999:blog-11722658.post-89244060740341273042016-12-13T13:41:00.000-05:002016-12-16T10:12:25.499-05:00Heap's Algorithm and Generating Perl Code From PseudocodeI've been researching recursion lately and in particular, permutations algorithms. This interest was spurred by a real-life case where such an algorithm would come in handy (combinations of @clients, @users, @tickets). I came across Wikipedia's entry for Heap's algorithm and the pseudocode illustrating the algorithm. I found the non-recursive version even more interesting specifically for its David M. Bradfordhttp://www.blogger.com/profile/04202175322890062001noreply@blogger.com2tag:blogger.com,1999:blog-11722658.post-28824612191135804452016-09-26T16:50:00.000-04:002016-09-26T16:50:36.694-04:00Tattletale VariablesSometimes you might be faced with a huge program that, somewhere, is changing a variable's value to something undesired. use Data::Dumper;
sub some_long_faraway_function {
my $href = shift;
# Pretend there's a lot of code here I don't want to sift through
$href->{bananas} = 'some bad value';
}
my $shopping_list = {
apples => 1,
pears => 3,
David M. Bradfordhttp://www.blogger.com/profile/04202175322890062001noreply@blogger.com0tag:blogger.com,1999:blog-11722658.post-58510002958775951572016-09-12T13:28:00.003-04:002016-09-12T13:28:52.301-04:00Build Your Memory PalaceEarlier this year, I gave a talk, "Building Your Memory Palace." Here are a few notes about the presentation:
The presentation itself
The original slides
Wikipedia has a good page on Method of loci
A Google search yields some good How-To videos
HowStuffWorks has a good write-up
David M. Bradfordhttp://www.blogger.com/profile/04202175322890062001noreply@blogger.com0tag:blogger.com,1999:blog-11722658.post-69301946461898842142015-08-26T16:34:00.002-04:002015-08-27T12:26:24.382-04:00Using Dispatch Tables To Improve Application SecurityUpdate: I have changed the title to "Using Dispatch Tables To Improve Application Security" for clarity.
At a previous job, I saw some code that asked the user which function they wanted to run and then executed a subroutine with that name. This code demonstrates why such a practice is bad: use strict;
use warnings;
sub greet { print "Hello!\n" }
sub inquireDavid M. Bradfordhttp://www.blogger.com/profile/04202175322890062001noreply@blogger.com4tag:blogger.com,1999:blog-11722658.post-21265227809930664052015-08-07T14:49:00.001-04:002015-08-07T15:00:25.373-04:00Accepting Input from Multiple SourcesOne of the corners I often paint myself into when developing a tool is only accepting one type of input, usually STDIN, the standard input stream, like a pipeline (ex: cat fruit.txt | grep apple) or a redirect (ex: grep apple < fruit.txt)What inevitably happens is I end up wanting the tool to work like any Unix tool and accept different kinds of input (filenames or arguments on the command David M. Bradfordhttp://www.blogger.com/profile/04202175322890062001noreply@blogger.com5tag:blogger.com,1999:blog-11722658.post-21356645540879092292015-04-26T15:27:00.003-04:002015-12-01T16:08:30.871-05:00Please ignore, just testing stylesThe Comments Section of a Blog is Important
Some people still don't read a blog's comments. I encourage you to do so if the topic interests you. The original post is not complete without the comments, because in them you will often find corrections to the original post or suggestions that improve upon it. Sometimes you will read comments that you feel add little, or, if it's especially David M. Bradfordhttp://www.blogger.com/profile/04202175322890062001noreply@blogger.com0tag:blogger.com,1999:blog-11722658.post-88595504075451679792015-04-05T21:23:00.000-04:002015-04-05T21:23:13.442-04:00Saving Vertical SpaceI was reviewing some code I had written for a simple RPG dice algorithm (although there's already a good module for this, Game::Dice) and I realized again that I have a prefererence for functions that can fit on one screen. One strategy is breaking up the code into smaller routines but I sometimes like to compact it vertically as much as possible first.
This function roll, given a string of "David M. Bradfordhttp://www.blogger.com/profile/04202175322890062001noreply@blogger.com11tag:blogger.com,1999:blog-11722658.post-73233683825804959052015-03-15T19:46:00.001-04:002015-03-15T19:47:00.676-04:00My Bad Communication SkillsA couple weeks ago I asked how you "join the conversation" but based on feedback I got, I don't think I communicated well. I think people thought I meant "which blogs do you read?" What I really meant was: when you write a blog entry, where do you post the link so that it's seen by people who are interested in that subject?So for example, when I write about Perl, I post to blogs.perl.org. I David M. Bradfordhttp://www.blogger.com/profile/04202175322890062001noreply@blogger.com0tag:blogger.com,1999:blog-11722658.post-58970687174511994112015-02-21T20:30:00.000-05:002015-02-21T20:30:02.443-05:00How do you join the conversation?
blogs.perl.org is great in that it's a stream of blog posts around a specific technology. Since I, like many of you, blog about other technologies too, I'd like to learn from you about other conversation streams. For me personally, the list of topics include:
Web development (JavaScript, CSS, etc)
Lifehacks
Unix, Linux, shell scripting
General tech / tech business
Database
I'll add what David M. Bradfordhttp://www.blogger.com/profile/04202175322890062001noreply@blogger.com2tag:blogger.com,1999:blog-11722658.post-70071591252980654212015-02-10T11:46:00.001-05:002015-02-10T11:46:58.843-05:00Fix Those Legacy Subroutines or Methods
Maybe you know the feeling… you go to add an option to that method or subroutine and… cue Jaws theme
sub update_shopping_cart {
my $cart_id = shift;
my $item = shift;
my $quantity = shift;
Argh. You don’t want your legacy code to break but you also don’t want to add a fourth unnamed parameter to the existing problem. And the solution is simple:
sub David M. Bradfordhttp://www.blogger.com/profile/04202175322890062001noreply@blogger.com3tag:blogger.com,1999:blog-11722658.post-71142005263497010902015-01-31T14:21:00.000-05:002015-01-31T14:25:05.095-05:00Command Line Project Manager (clpm) v1.0.1 released
clpm is designed to make managing sets of files easier at the Unix command line. It's free and open source. Please view the demo here. Find more info at http://tinypig.com/clpm
David M. Bradfordhttp://www.blogger.com/profile/04202175322890062001noreply@blogger.com0tag:blogger.com,1999:blog-11722658.post-46384179308616405302015-01-18T15:29:00.000-05:002015-01-18T15:33:41.316-05:00Call for help with open source project "CLPM"
CLPM is my “Command Line Project Manager”. It’s a tool I wrote and have been using myself for several years now, and I am releasing it in the hope that others might find it useful.
Also, if you have been looking for an open source project to contribute to, here’s your chance! I don’t care what your level of experience is, if you think you have a useful comment or contribution, I’d like to hearDavid M. Bradfordhttp://www.blogger.com/profile/04202175322890062001noreply@blogger.com0tag:blogger.com,1999:blog-11722658.post-78938188504796908942015-01-08T12:16:00.000-05:002015-01-08T12:16:20.177-05:00CPAN Pull Request Challenge: The Pull RequestIn my last report, I had received an ok from the module’s author to proceed with a pull request. The first thing I did, as I had with the modules referenced by Devel::StackTrace::WithLexicals, is to write an example for myself so I could see how it worked. Working from the synopsis, I had a difficult time writing a working piece of code. It occurred to me that this was the very place I David M. Bradfordhttp://www.blogger.com/profile/04202175322890062001noreply@blogger.com0tag:blogger.com,1999:blog-11722658.post-71432812215523234932015-01-02T19:17:00.000-05:002015-01-08T11:56:43.387-05:00My First Day on the CPAN Pull Request Challenge
So yesterday, I started on the CPAN Pull Request Challenge. I was assigned the module Devel::StackTrace::WithLexicals, so, I read the doc and installed it.
The doc references two other modules’ influence: Devel::StackTrace and PadWalker
So I figured I’d have a look at those first. Combining two examples from the doc for Devel::StackTrace, I wrote this:
#!/usr/bin/perl
use strict;
use David M. Bradfordhttp://www.blogger.com/profile/04202175322890062001noreply@blogger.com1tag:blogger.com,1999:blog-11722658.post-19983504191761280242014-12-21T15:54:00.000-05:002014-12-29T17:40:07.523-05:00Vim Macros: A More Complicated ProblemHere’s a more difficult problem than my previous post on Vim macros: I collected a bunch of links to post on my website using a simple 2-column table:
blogs.perl.org |http://blogs.perl.org/mt/mt-cp.fcgi?__mode=view&id=3159
Wiki |http://tinypig.pbworks.com/
Tumblr |http://tinypigdotcom.tumblr.com/
WordPress |http://tinypig.wordpress.com/
Perl Monks |http://David M. Bradfordhttp://www.blogger.com/profile/04202175322890062001noreply@blogger.com0tag:blogger.com,1999:blog-11722658.post-34928671157335561242014-12-17T17:44:00.000-05:002014-12-17T17:46:31.712-05:00Fixing Multiple Lines with Vim Macros
Sometimes I have a tedius code change where I have to perform the same task on multiple lines in vim and I can’t use ‘.’ because each line is a little different or the changes are too complicated. In this situation, I use a recursive macro in vim.
Here, I started with a list of items that I want to turn into a dispatch table - the code itself isn’t important here, it’s the transformation that IDavid M. Bradfordhttp://www.blogger.com/profile/04202175322890062001noreply@blogger.com0tag:blogger.com,1999:blog-11722658.post-3647901602338709592014-12-05T11:59:00.000-05:002014-12-05T12:01:43.994-05:00Using Files for Inter-process Communication
I was abusing a test suite I had written to add test data to the database and I was running it inside an infinite loop because it was going to take a while to generate the 10,000 records I wanted. When it came time to stop, I had difficulty either control-c-ing or killing the right processes to get it to quit, so the second time I ran it, I did this instead:
while true
do
if [ -f $HOME/David M. Bradfordhttp://www.blogger.com/profile/04202175322890062001noreply@blogger.com0tag:blogger.com,1999:blog-11722658.post-21859365071255153242014-12-04T15:36:00.002-05:002014-12-04T15:37:44.588-05:00XML Nested CDATA sections
"Nested CDATA sections are not allowed." But I need that! I have XML that contains XHTML which in turn has a CDATA section of its own. Surprisingly I found the answer at Wikipedia.
Here's my XML:
<apple>
<banana>
<![CDATA[
<script type="text/javascript">
/* <![CDATA[ */
var language = "en";
/* ]]]]><![CDATA[> */
</scriptDavid M. Bradfordhttp://www.blogger.com/profile/04202175322890062001noreply@blogger.com0tag:blogger.com,1999:blog-11722658.post-16221694054696451412014-07-08T14:36:00.000-04:002014-07-24T11:39:56.683-04:00Class AbuseThis situation arose recently and it got me curious...
What if you needed to access an algorithm in a very simple class method, but the functionality of the class is significant and would be a pain in the ass to set up and create an object in, not to mention resource-intensive, all to use one small function...
You could copy the algorithm, but what if it changes? Then you'll have to changeDavid M. Bradfordhttp://www.blogger.com/profile/04202175322890062001noreply@blogger.com2tag:blogger.com,1999:blog-11722658.post-21084050321804710542014-06-08T17:26:00.000-04:002014-06-08T17:39:53.069-04:00My First Crontab
Ok, it's not the first crontab I've ever written. This is my crontab template that I just created for mysellf because even though I've been using cron since the late 80s, I still keep forgetting which side is minutes. How sad is that?!?
I copied the nice format from Wikipedia's cron page.
The first line (the only line that isn't a comment, and therefore an actual crontab entry) is my initialDavid M. Bradfordhttp://www.blogger.com/profile/04202175322890062001noreply@blogger.com0tag:blogger.com,1999:blog-11722658.post-53510483702127049672014-05-08T17:52:00.002-04:002014-05-09T16:55:19.829-04:00When a Tradeoff is not a Tradeoff
Years ago, I wrote a post on clever code and advised against it, saying that instead we should strive to make our code maintainable. Since then, I gave a talk at DCBPW called Writing Maintainable Perl. During the talk, I broke a single line of code into multiple lines of code. A member of the audience pointed out that this could have performance ramifications because every time there is a David M. Bradfordhttp://www.blogger.com/profile/04202175322890062001noreply@blogger.com0tag:blogger.com,1999:blog-11722658.post-14582940562968483132014-02-24T21:11:00.001-05:002014-02-24T21:13:05.169-05:00Convert Unix man page to PDF
Thanks to this post by Aron at True EDGE, I learned how to convert a Unix man page to a PDF file:
man -t bash | ps2pdf - bash.pdf
Combined with my iPad, and the GoodReader app (in which I can sync a subdirectory from my Dropbox), and I can save man pages for reading later in a more comfortable setting.
David M. Bradfordhttp://www.blogger.com/profile/04202175322890062001noreply@blogger.com0tag:blogger.com,1999:blog-11722658.post-51150625683032950142014-02-22T14:16:00.002-05:002014-03-08T15:39:13.324-05:00Changing the Lock Screen Wallpaper in Lubuntu
I noticed that after changing my desktop wallpaper in Lubuntu 13.10 "saucy" (right click on the desktop and choose Desktop Preferences, Appearance tab), that the image on my lock screen had not changed. I don't love that wallpaper, so I wanted to change it. After searching the web and not finding a way to do so, I tried a few things and finally ran a locate for "wallpaper" finding, David M. Bradfordhttp://www.blogger.com/profile/04202175322890062001noreply@blogger.com1tag:blogger.com,1999:blog-11722658.post-20940793177407220592014-01-27T21:40:00.001-05:002014-01-27T21:42:07.749-05:00Confusion Between Target Host and Local Host in SSH Tunnel
I was trying to tunnel VNC connections over SSH and I encountered a problem caused by a mistake I always make, setting up the SSH tunnel on the host that's going to VNC-view the target host:
$ ssh -f user@target_host -L 5905:target_host:5901 -N
I try to connect via this tunnel and I see:
channel 2: open failed: connect failed: Connection refused
I should have created the tunnel like thisDavid M. Bradfordhttp://www.blogger.com/profile/04202175322890062001noreply@blogger.com0tag:blogger.com,1999:blog-11722658.post-69087106411939933152013-12-10T23:00:00.000-05:002013-12-10T23:03:57.073-05:00xwords
An "xword" is part of a system I created to automate some of the routine things in my life. It is somewhat similar in function to a hashtag, in that it categorizes text without being part of the text itself.
To follow my examples, it helps to know that I use the iPhone app Captio (which I recommend) to send myself emails from my phone. It will send every email you create to the same address David M. Bradfordhttp://www.blogger.com/profile/04202175322890062001noreply@blogger.com0