A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

Jump to

Quick reference

Table parsing, old friend

Some comments I found in code once:

    # Table parsing, old friend... We have spent many long evenings together,    
    # working through your broken tables, talking about your issues with         
    # implicit thead elements, and your quirky tr/th heuristics. But ... but     
    # but it's time for us to move on. We can't keep doing this. It's not you,   
    # it's me...                                                                 
    #                                                                            
    # I've ... I've met someone new. She's beautiful, she's modern, and she's    
    # elegant. She's logical, she's clean, and she's well-structured, if you     
    # know what I mean...                                                        
    #                                                                            
    # She always puts a single tr in her thead's, and what's more, she's always  
    # got a thead. Her tbody's support her tr's, and she never forgets them.     
    # Life is simpler now. Better. Less miscommunication, fewer arguments over   
    # what defines a header row. Sometimes it lasts in parsing code, but         
    # sometimes, it hurts instead...                                             
    #                                                                            
    # IF YOU EXTEND THIS CODE TO SUPPORT YOUR BROKEN HTML
    # TABLES I WILL FIND YOU. YOU HAVE BEEN WARNED!  

Thanks to Pete for much amusement.

How to implement a link table in DBIC

# Define many-to-many relationship between foo and bar tables
# using the foos_bars linking table

# in the database:


TABLE foo ( id INTEGER, something TEXT );
TABLE bar ( id INTEGER, another_thing TEXT );
TABLE foo_bar ( foo_id INTEGER, bar_id INTEGER );


# in Result/Foo.pm:

__PACKAGE__->many_to_many(
    'bars', # name of the relationship you're creating in foo
    'foos_bars', # name of the relationship in foo that points to the link table
    'bar' # name of the relationship to bar on the link table
);


# in some nearby code:

@bars = $foo->bars;


CPAN docs

Shuffle lines in bash

#!/bin/bash
# Print out the lines from a file, in a random order
FILE=$1
if [[ -z $FILE ]]
then
    echo "usage: shuffle [filename]"
    exit 1
fi
for i in `cat $FILE`; do echo "$RANDOM $i"; done | sort | sed -r 's/^[0-9]+ //'

Thanks

Display postgres enum values

  • To see all enums:
select n.nspname as enum_schema,  
    t.typname as enum_name,
    string_agg(e.enumlabel, ', ') as enum_value
from pg_type t 
    join pg_enum e on t.oid = e.enumtypid  
    join pg_catalog.pg_namespace n ON n.oid = t.typnamespace
group by enum_schema, enum_name;
thanks, StackOverflow

  • To see one enum:
SELECT enumlabel  FROM pg_enum WHERE enumtypid = 'your_enum_here'::regtype;

Use variables with Postgres, just like MySQL

It's a little more difficult than MySQL, as you have to create a function to contain the logic:

DROP FUNCTION get_column(integer);
CREATE FUNCTION get_column(row_id integer) RETURNS text AS $$
DECLARE
    t1_row foo%ROWTYPE;
BEGIN
    SELECT * INTO t1_row FROM foo WHERE foo.status != 'closed' limit 1;
    RETURN t1_row;
END;
$$ LANGUAGE plpgsql;

SELECT get_column(3);