Skip to main content

Posts

Showing posts with the label perl

Handy Perl Functions: Automatically create a form from a database table

Back after a much needed vacation with another perl code snippet. This one creates a simple form using information pulled from the structure of a database table. The form it creates is not meant to be the end product - rather it gets a lot of the "grunt work" done, and you can then edit the resulting form to change fields to drop-downs, remove fields that shouldn't be edited, etc. It will also try to guess at a decent label for each field, based on the table name. This script uses the same get_fields() function that outlined in my previous post. As always, I'm sure this isn't the "perfect" code, and of course it can be improved - but it does work, and may perhaps be a useful starting block for someone else... Usage - pretty simple - run the script and give it a database table name to use. It'll spit the html back out, which you can save in a text file or whatever. #!/usr/bin/perl # creates a basic form from the database table specified use DBI; # ...

Handy Perl Functions: get_fields()

This is the first of what may be a recurring theme - handy custom perl functions. These are custom pieces of code that I find myself using in a wide variety of places, both in dynamic websites and in maintenance scripts. This first one is called get_fields() and its function is quite simple - it returns an array of field names for the table name passed to it. I've used it for things like generating edit/new record form templates, or when writing scripts to convert from database format to another, etc. It uses the perl DBI module, and assumes that $dbh is the database handle that has been set up for use. #--------------------------------------------------------------- sub get_fields { # returns array of fields for the table name passed to it my $table = $_[0]; my (@fields, @tmp) = (); my $qry = "describe $table"; $qry = $dbh->prepare($qry); $qry->execute; while (@tmp = $qry->fetchrow_array) { ...

Cleaning content from OpenOffice using Perl

Open office is great software for a number of things - I use it as my office software instead of paying a premium for Microsoft office. But one thing it's not so hot at is converting documents to clean HTML. And one of the main things I use it for is adding content to sites that clients send me in word files or excel spreadsheets. Of course, you can always cut and paste, but that loses a lot of formatting. For example, if the content uses a lot of italics, bold text, etc. it can be a huge pain to go back and put all that back in. Another common situation is a client sending some sort of tablular data in a spreadsheet - for example a list of events. It's the kind of data that can change a lot, and it also needs to be in a table with some decent formatting to be usable. Doing it manually is a lot of grunt work. But grunt work is what computers excel at, and I'm not very good at. So I've developed a number of perl scripts to help streamline this kind of job. I'll go ...

Helpful Perl -> PHP cheat sheet

Found this helpful cheat sheet and thought it might help someone else out there. I'm in the middle of converting the first of what will be several perl-driven websites to PHP so they can be moved to a new server. This is not because of any problem with Perl (which I still prefer) but because at their new home the policy is only PHP-driven websites, for ease of maintenance, which I think is reasonable. But the conversion is kind of a hassle - I've been doing Perl for so long that I can pretty much sit down and write it without needing much reference - not so for PHP. And worse, the two are so similar that I keep thinking something will work in PHP only to run into weird (to me) syntax issues. So it goes. At least I'm paid hourly.