in reply to How to replace envs in path?

happy half way to midweek ovedpo15,

Are you rolling your own shell script parser, or sending in strings of symbolic scalar refs from another part of your perl script?

Replies are listed 'Best First'.
Re^2: How to replace envs in path?
by ovedpo15 (Pilgrim) on May 09, 2022 at 12:06 UTC
    Hi!
    Thanks for the replay. I get a custom list of paths and I create a structure which represents the filesystem. I'm trying to support environment variables inside the list of paths so hence the question. Just want to support paths like $HOME/work_dir/${TOOL_NAME}/$VERSION.

      the paths you do no want to expand are any where the scalar sigil $ is not immediately after the filepath separator. In which case you can allow these by matching for that construct.

      my $fps = 'filepathseparator'; m! $fps # your file path separator \Q$\E # quoted scalar sigil (shell variable expansion) \{? # start shelling out for a return value ? ([^\W]+) # your environment variable, not containing, # one or more non-word characters \}? # end shelling out for a return value ? !x

      This should match both cases where the curly braces are or are not used while constraining the variable to starting immediately after the file_path_separator (no mid/end word sigils match), and not allowing the path word to contain any non word characters.