Pg::hstore(3pm)

πŸ“¦
version 1.06 Β· 5 versions
, in debian trixie, ubuntu resolute

πŸ’– 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.

  • NULL values will be converted to Perl undef
  • NULL or empty keys will be ignored
  • Returns hash ref with key => value pairs
  • Can return undef on 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.

  • undef values will be converted to NULL
  • Empty or undef keys will be ignored
  • Returns hstore-serialized string
  • Can return undef on 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 (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