Pg::hstore(3pm)
π Pg::hstore β bestie edition π
okay so this is Pg::hstore, a Perl extension for encoding/decoding postgresql's hstore data type. yes. that's the whole vibe. postgres has a lil key-value blob type and this module is the translator so your Perl brain and your DB brain can actually talk to each other π«Ά
β¨ NAME β¨
Pg::hstore β Perl extension for encoding/decoding postgresql's hstore data type. that's it, that's the name, we're not doing anything fancy here π
β¨ SYNOPSIS β¨
like, just look at it:
use Pg::hstore;
...
my $row = $sth->fetchrow_hashref; #Getting row from DB
print "hstore data: ", $row->{addinfo}, "\n";
my $addinfo = Pg::hstore::decode($row->{addinfo}); #Decoding into perl hashref
#now $addinfo is 1-level hash ref with key->value
print "addinfo->'full_name' = ", $addinfo->{full_name}, "\n";
...
#Updating hstore. This type of updating can wipe other process work on addinfo field
$addinfo->{full_name} = "John the ripper";
$dbh->do("update users set addinfo=? where id=?", undef,
Pg::hstore::encode($addinfo), 123);
so you pull a row, you grab that addinfo column, you decode it into a real Perl hashref, and now $addinfo->{full_name} justβ¦ works. main character behaviour tbh π
and going the other way? you encode your hashref back into an hstore string and shove it into your update. slay.
β οΈ but bestie, read the comment: this kind of updating can wipe other process work on addinfo field. like, if someone else edited it between your read and your write, their stuff is gone. it's giving read-modify-write race condition π. the original says it, i'm just saying it louder.
β¨ DESCRIPTION β¨
two subs. one in each direction. that's the whole thing. π
π decode(string)
Decode given HSTORE value. that's the job.
NULLvalues will be converted to PerlundefNULLor empty keys will be ignored- Returns hash ref with
key => valuepairs - Can return
undefon internal error - Keys and values will have same UTF8 flag as incoming string
so it hands you back a hashref, and the UTF8 flag on the keys and values matches whatever the incoming string had. no surprises, no sneaky re-encoding. we love a consistent queen β¨
π encode(hashref)
Encodes given hash ref to HSTORE-format string. the reverse trip, iconic.
undefvalues will be converted toNULL- Empty or
undefkeys will be ignored - Returns hstore-serialized string
- Can return
undefon internal error - String will have UTF8 flag ON if any of hashref values have it
so if any single value in your hashref is carrying the UTF8 flag, the whole output string gets it too. noted. π
β¨ EXPORT β¨
None by default. like, it's not just gonna hand you things unprompted, that's rude. π
but you can export hstore_encode and hstore_decode subs:
use Pg::hstore qw/hstore_encode hstore_decode/;
then you can just call hstore_encode(...) and hstore_decode(...) directly like it's nothing. periodt. π
β¨ BUGS β¨
None known. π¦
but feel free to clone/contribue from
https://bitbucket.org/PSIAlt/p5-pg-hstore
(that's "contribue" exactly as written, no cap, i'm not touching the spelling, it's part of the lore now π)
β¨ COPYRIGHT AND LICENSE β¨
Copyright (C) 2011 by Alt
and now the license, which is genuinely the most unhinged and delightful thing in this whole page, so here it is verbatim, preserved like a museum piece:
----------------------------------------------------------------------------
"THE BEER-WARE LICENSE"
As long as you retain this notice you can do whatever you want with this stuff.
If we meet some day, and you think this stuff is worth it, you can buy me a beer
in return.
----------------------------------------------------------------------------
beer-ware. BEER-WARE. π like, do whatever you want with this stuff, just keep the notice, and if we ever meet up and you're feeling generousβ¦ buy me a beer. that's the entire legal framework. i'm obsessed. no notes. π₯
and that's the whole page bestie π this was Pg::hstore(3pm) β perl v5.40.1, dated 2025-03-07, from the User Contributed Perl Documentation. go forth, encode, decode, hydrate responsibly πΈβ¨π¦
π¦ where this page comes from
- debian trixie: libpg-hstore-perl 1.06-5 Β· Beerware
- ubuntu resolute: libpg-hstore-perl 1.06-5 Β· Beerware
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms