Tangra...:Scalar(3pm)

πŸ“¦
version 2.12 Β· 3 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– Tangram::Type::Hash::Scalar β€” the manual page, but make it cute

okay bestie, gather round, we're reading the Tangram::Type::Hash::Scalar page together and honestly? it's a bop. it's part of the Perl docs the community contributed, and what it does is map a Perl hash that's all scalars β€” keys AND values β€” into your Tangram schema. no cap. let's get into it πŸ’…


✨ NAME ✨

Tangram::Type::Hash::Scalar β€” map Perl hash of scalar keys and values

like. the name literally tells you everything. a hash. of scalars. mapped. iconic behavior honestly.


πŸŽ€ SYNOPSIS πŸŽ€

use Tangram::Core;
use Tangram::Type::Hash::Scalar; # always

$schema = Tangram::Schema->new(
   classes => { NaturalPerson => { fields => {

   flat_hash =>
   {
      opinions =>
      {
         table => 'NP_ops',
         key_sql => 'VARCHAR(10)',
         type => 'int',
         sql => 'NUMERIC(1)',
      },

      lucky_numbers => 'int', # use defaults
   },

see how use Tangram::Type::Hash::Scalar; says # always? yeah. always. not "sometimes", not "when you feel like it". always. it's giving devoted girlfriend energy and we respect it 🫢


πŸ¦‹ DESCRIPTION πŸ¦‹

so what does this thing actually do. it maps references to a Perl hash. the persistent fields get grouped up in a hash under the flat_hash key inside the field hash. cute little family reunion.

🚨 the big rule: the hash may contain, as keys and values, only 'simple' scalars β€” think integers, strings, real numbers. it may not contain references. nope. not happening. if you want a hash of objects (which is a whole different vibe), go look at Tangram::Type::Hash::FromMany and Tangram::Type::Hash::FromOne instead. those are the object-hash girlies.

Tangram uses a table to save the state of the collection. the table has three columns, which contain:

  • the id of the container object
  • the key of the element in the hash
  • the value of the element

three columns. that's it. minimalism. the main character doesn't overpack πŸ’…

🌸 the field descriptor situation

the field names get passed in a hash that associates a field name with a field descriptor. the field descriptor may be either a hash or a string. and the hash uses the following fields:

  • key_type
  • key_sql
  • type
  • sql
  • table

that's the whole roster, bestie. five options. know them, love them.

The optional fields key_type and type specify the key and value types of the hash. if the type is string, Tangram quotes the values as they are passed to the database. and here's the thing β€” not specifying a type is exactly equivalent to specifying string. so if you skip it, congrats, you got string. surprise! πŸŽ‰

The optional field table sets the name of the table that contains the elements. this defaults to 'C_F', where C is the class of the containing object and F is the field name. so it names itself after its parents. very "named after both of you" of it 😭

The optional fields key_sql and sql specify the type that deploy() (see Tangram::Deploy) should use for the column containing the key and value of the hash. if this field is not present, the SQL type is derived from the type field: if type is string (or is absent) VARCHAR(255) is used; otherwise, the type field is interpreted as a SQL type.

okay so to recap that lil chain of custody:

  • no key_sql/sql given?
    • type is string or missing β†’ VARCHAR(255) ✨
    • otherwise β†’ whatever you put in type is straight-up read as a SQL type

and finally, the shortcut: if the descriptor is a string, it is interpreted as the value of the type field and all the other fields take the default value. one string, everything else auto-piloted. it's giving low-effort-high-reward and honestly, slay πŸ“


πŸ‘‘ AUTHOR πŸ‘‘

This mapping was contributed by Gabor Herr <herr@iti.informatik.tu-darmstadt.de>

thank you Gabor, we love you, this is your legacy, you ate and left no crumbs πŸ’–


and that's the whole page bestie!! no notes, no missing flags, we kept every single detail because that's the rule β€” vibe changes, facts don't. πŸ”₯

this came to you from perl v5.36.0, dated 2022-10-16, and yeah the title up top does look like Tangra...:Scalar(3pm) because the terminal truncates it, but you and me know the truth: it's Tangram::Type::Hash::Scalar(3pm). bye bye now, go map some scalars πŸ¦‹βœ¨πŸ’œ
πŸ“¦ where this page comes from