Rex::Shared::Var(3pm)

πŸ’– NAME πŸ’–

Rex::Shared::Var β€” share your variables across Rex tasks, bestie, no cap 🫢


🌸 DESCRIPTION 🌸

Okay so picture this: you've got a bunch of Rex tasks and they're all refusing to talk to each other like they're in a group chat with read receipts off. Rex::Shared::Var fixes that. It lets you share variables across Rex tasks with a lil assist from Storable πŸ“¦, and it does it by keeping a file called vars.db.$PID in your local directory β€” where $PID is the PID of the parent process. That's literally it. Slay.


✨ SYNOPSIS ✨

 BEGIN {                           # put share in a BEGIN block
   use Rex::Shared::Var;
   share qw($scalar @array %hash); # share the listed variables
 }
heads up bestie: the share call goes in a BEGIN block. That part is non-negotiable πŸ’…

😭 LIMITATIONS 😭

Not everything is perfect, and honestly, same. Right now, nesting data structures only works if either:

  • the assignment happens at the top level of the structure, or
  • the nested structures are also shared variables

Like, for example:

 BEGIN {
   use Rex::Shared::Var;
   share qw(%hash %nested);
 }

 # this doesn't work as expected
 $hash{key} = { nested_key => 42 };
 $hash{key}->{nested_key} = -1; # $hash{key}->{nested_key} still returns 42

 # workaround 1 - top level assignments
 $hash{key} = { nested_key => 42 };
 $hash{key} = { nested_key => -1 };

 # workaround 2 - nesting shared variables
 $nested{nested_key}      = 42;
 $hash{key}               = \%nested;
 $hash{key}->{nested_key} = -1;

So yeah β€” that deep-nesting edit? It just does not stick, $hash{key}->{nested_key} still returns 42 and you're left staring at your terminal like πŸ§β€β™€οΈ. Use workaround 1 or workaround 2 and you're golden 🌈


πŸ‘‘ METHODS πŸ‘‘

πŸŽ€ share

Shares the passed list of variables across Rex tasks. Should be used in a BEGIN block. (Yes, again. It's that serious.)

 BEGIN {
   use Rex::Shared::Var;
   share qw($error_count);
 }

 task 'count', sub {
   $error_count += run 'wc -l /var/log/syslog';
 };

 after_task_finished 'count', sub {
   say "Total number of errors: $error_count";
 };

Look at that β€” one task bumps $error_count, another task reads it back like a true bestie who actually remembers what you told her 🫢 iconic behaviour fr fr.


and that's the whole vibe, periodt βœ¨πŸ’…πŸ¦‹

This page came to you courtesy of the Perl docs, version perl v5.40.1, dated 2025-10-03 β€” Rex::Shared::Var(3pm), out πŸ’œ

πŸ“¦ where this page comes from