Search code examples
perlenvironment-variablesbareword

Setting bareword environment variable not allowed while strict subs in use


In order for my Perl script to run, I need to set an environment variable on the OS to point to our dev servers:

export SERVER=DEV

However, once the script is distributed to our users, I don't want them to need to export this environment variable each time. Therefore, I thought I would set the environment variable near the beginning of my script. So I added this:

$ENV{SERVER}=DEV;

However, my script won't allow this because of strict subs:

Bareword "DEV" not allowed while "strict subs" in use

I've been assured by my department supervisor we must use strict in our scripts, so there's no way of getting around that constraint. Is there a proper way to set bareword environment variables that I am overlooking?


Solution

  • Environment variables are strings.

    You have many options to create the string DEV in bash. Primarily, you have

    • DEV
    • "DEV"
    • 'DEV'

    You have many options to create the string DEV in Perl. Primarily, you have

    • "DEV", qq{DEV}
    • 'DEV', q{DEV}

    So,

    $ENV{SERVER} = "DEV";