Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

What's the best way to convert a non interpolated single-quoted string into an interpolated double-quoted string?

by emilbarton (Scribe)
on Mar 15, 2012 at 19:50 UTC ( #959845=perlquestion: print w/replies, xml ) Need Help??
emilbarton has asked for the wisdom of the Perl Monks concerning the following question:

I was trying to make a module less private replacing my own path in a config file by an '$ENV{HOME}/path' when I realised that I wasn't able to convert it easily, after XML import, to an "$ENV{HOME}/path" interpolative context.

First I thought I had forgotten some basic trick, but chatter box friends advise to use regex or to investigate more. How would YOU do?

  • Comment on What's the best way to convert a non interpolated single-quoted string into an interpolated double-quoted string?

Replies are listed 'Best First'.
Re: What's the best way to convert a non interpolated single-quoted string into an interpolated double-quoted string? (smor)
by tye (Sage) on Mar 15, 2012 at 19:59 UTC
    s/\$ENV{(\w+)}/$ENV{$1}/ge;

    That doesn't solve the 'problem' of wanting to have a literal '$ENV{X}' kept unchanged for some reason. That seems unlikely enough of a desire that I'm not sure I'd even do anything about it. Though I might prevent substitution if no such environment var is set:

    s/\$ENV{(\w+)}/ exists $ENV{$1} ? $ENV{$1} : "\$ENV{$1}" /ge;

    - tye        

      Nice!
Re: What's the best way to convert a non interpolated single-quoted string into an interpolated double-quoted string?
by jeffa (Bishop) on Mar 15, 2012 at 19:58 UTC

    I would simply store the relative "path" part and leave the $ENV{HOME}/ out completely. Then, after you parse the XML file you can prepend the value of $ENV{HOME}, perhaps with a rule that only if the value is relative and not absolute.


    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Yes I wanted the user to be able to change the path afterwards through some specific pref pane without being enclosed at $HOME, your solution might do it well.
Re: What's the best way to convert a non interpolated single-quoted string into an interpolated double-quoted string?
by GrandFather (Sage) on Mar 16, 2012 at 05:32 UTC

    I that situation I use place holder text that is replaced at run time using a regex and a hash lookup. Something like the following:

    my %subs = (home => 'HomePathFromConfigOrEnv'); my $keyMatch = join '|', keys %subs; my $cfgStr = '!home!/path'; $cfgStr =~ s/!($keyMatch)!/$subs{$1}/g; print $cfgStr;

    Prints:

    HomePathFromConfigOrEnv/path
    True laziness is hard work
Re: What's the best way to convert a non interpolated single-quoted string into an interpolated double-quoted string?
by LanX (Bishop) on Mar 15, 2012 at 20:00 UTC
    You can use eval, but be aware about the risk of injections if others edit your config.

    Cheers Rolf

      Yes it's clearly the basic trick, except that I couldn't use eval in this particular case.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://959845]
Approved by ww
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (10)
As of 2017-10-22 21:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My fridge is mostly full of:

















    Results (275 votes). Check out past polls.

    Notices?