2023 twenty-four merry days of Perl Feed

A Dotenv Carol

ENV::Util, dotenv, .env, configuration, setup, config, microservices, docker, containers, devops, deployment - 2023-12-20

In the chilling corridors of Scrooge & Marley Software Solutions, Ebenezer Scrooge sat hunched over his laptop, hands clutching his hair, overwhelmed by failed deployments of his containers. Suddenly, a ghostly figure materialized before him – the Ghost of Configurations Past. It pointed at the stack of old, unwieldy JSON and YAML files in Scrooge's applications. "Behold, Ebenezer," moaned the specter, "the complexity and confusion of your past configurations. Remember the days of endless nested structures and tedious manual edits."

As the ghost swept Scrooge through the digital corridors of time, he saw himself wrestling with intricate configurations, wasting precious hours on syntax errors and misalignments. "There must be a simpler way," the ghost whispered.

Next appeared the Ghost of Configurations Present, adorned with snippets of Perl code and a merry demeanor. It took Scrooge to witness scenes of joyous developers using dotenv files – simple, readable, and devoid of unnecessary complexity. "Behold the present, Ebenezer!" the ghost declared. "Look at the ease with which developers manage all those different app environments. No more verbosity, no more headaches. Dotenv files bring clarity and simplicity to configuration management.".

"But how were they even able to change all that code to use .env files?", Ebenezer scoffed. "It must have cost a fortune to migrate!". The spectre quietly directed him to an open IDE, where a single line simply read:

    use ENV::Util -load_dotenv;

As Scrooge squinted at the line, the ghostly voice resonated. "ENV::Util is a lightweight module with zero dependencies. And just by adding that line, your %ENV will be populated with all the variables defined in your dotenv file, making it ready to use throughout your app." The old man remained skeptical. "Fine, but what if I want a configuration hash that's specific to my app, that I can for example iterate on, knowing it will only contain relevant settings?" The ethereal entity waved him to another terminal. "You mean like so?"

    my %config = ENV::Util::prefix2hash( 'MYAPP_' )

"This will parse %ENV and return a key/value hash containing just the variables prefixed by 'MYAPP_'. It will even remove the prefix and lowercase variable names so they become more Perlish. This way a dotenv file with a line like this:"

    MYAPP_DB_SERVER_IP=127.0.0.1

"Can be accessed inside that hash as:"

    $config{db_server_ip}

Ebenezer was speechless. Then he remembered one of his past hurdles with configuration files. "That doesn't solve my main debugging issue! When thing go wrong, and in my line of business you know they will eventually, you sometimes need to dump all your configuration data, but that puts a lot of secrets in plain text in my logfiles!". With an unchanged demeanor, the apparition replied: "Then instead of dumping %ENV you can dump ENV::Util::redacted_env, which will mask the values of any keys that look sensitive, like the ones matching 'ID', 'EMAIL', 'TOKEN' or 'PASS':"

    use ENV::Util -load_dotenv;
    use Data::Printer;

    my %redacted = ENV::Util::redacted_env();
    p %redacted;

"You can even add your own rules as to what gets redacted and what doesn't. With ENV::Util, there really is no excuse to avoid dotenv files."

"It's a Christmas miracle!" Scrooge exclaimed, his eyes gleaming with newfound understanding.

But the Ghost of Configurations Yet to Come loomed ominously. It revealed a desolate future where Scrooge clung stubbornly to traditional configuration files in his apps, leading to confusion, inefficiency, disgruntled developers and... overtime.

"No more!" cried Scrooge, awakening from his spectral journey. "I have seen the error of my ways. With ENV::Util, I shall embrace the simplicity of dotenv files and bid farewell to the chains of complexity that bind my code."

And so, on that Christmas morning, Scrooge used ENV::Util and replaced his verbose configurations with a neat .env file. As the clock struck midnight, signaling the dawn of a new day, Scrooge's software team gathered, their faces beaming with joy. The office, once shrouded in the darkness of cumbersome configurations, now radiated the festive glow of efficient and readable dotenv files.

Gravatar Image This article contributed by: Breno G. de Oliveira <garu@cpan.org>