itisrich has asked for the wisdom of the Perl Monks concerning the following question:

Here is what I'm trying to do:

my $foo = "stuff"; open FD, "file.txt"; @data = <FD>; close FD; foreach (@data) { s/XXYY/$foo/; print; }

seems simple enough. I've done this before with no problems. For some reason, in this particular project, XXYY is not getting replaced by the contents of $foo, it is getting replaced by the actual variable name.

what was "XXYY" is now being printed as "$foo". Even though I have never had to do it before, I tried adding an "ee" at the end of the s///, but it made no difference.

Any help would be much appreciated.

-rich

Replies are listed 'Best First'.
Re: s/this/$that/ Gives Variable Name, Not Contents of Variable
by Corion (Patriarch) on Oct 07, 2013 at 15:27 UTC

    I can't reproduce that:

    my $foo = "stuff"; @data = <DATA>; foreach (@data) { s/XXYY/$foo/; print; } __DATA__ <XXYY> XXYY XXYY test

    gives the following output:

    >perl -w tmp.pl <stuff> stuff XXYY test

    Maybe you are looking at the wrong output file?

    In any case, you most likely want a /g at the end of your substitution, so that the second input line becomes stuff stuff on output.

Re: s/this/$that/ Gives Variable Name, Not Contents of Variable
by toolic (Bishop) on Oct 07, 2013 at 15:48 UTC
    The behavior you describe would be possible if you were escaping the $. Is that really your code, or do you actually have:
    s/XXYY/\$foo/;
Re: s/this/$that/ Gives Variable Name, Not Contents of Variable
by VinsWorldcom (Prior) on Oct 07, 2013 at 15:31 UTC

    No idea - it works for me. Windows 7 x64 with Strawberry 5.16.1:

    VinsWorldcom@C:\Users\VinsWorldcom\tmp> perl -v This is perl 5, version 16, subversion 1 (v5.16.1) built for MSWin32-x +64-multi-thread [...] VinsWorldcom@C:\Users\VinsWorldcom\tmp> cat test.pl my $foo = "stuff"; open FD, "file.txt"; @data = <FD>; close FD; foreach (@data) { s/XXYY/$foo/; print; } VinsWorldcom@C:\Users\VinsWorldcom\tmp> cat file.txt XXYY VinsWorldcom@C:\Users\VinsWorldcom\tmp> test.pl stuff
Re: s/this/$that/ Gives Variable Name, Not Contents of Variable
by AnomalousMonk (Archbishop) on Oct 08, 2013 at 00:21 UTC
    ... I tried adding an "ee" at the end of the s/// ...

    In addition to 'e's, it sometimes helps to add a couple of 'i's and an 'o':  s///eieio

Re: s/this/$that/ Gives Variable Name, Not Contents of Variable
by AnomalousMonk (Archbishop) on Oct 07, 2013 at 22:13 UTC

    In addition to escaping the sigil in  $foo in double-quotish interpolation (e.g., "\$foo"), another way to get the effect you describe is to use single-quotes as the  s/// replacement delimiters:

    >perl -wMstrict -le "my $foo = 'stuff'; my $str = 'xxx XXYY yyy'; $str =~ s'XXYY'$foo'; print qq{'$str'}; " 'xxx $foo yyy'

    But of course single-quotes are not what you have in your OP. Are you fooling us — or yourself?

Re: s/this/$that/ Gives Variable Name, Not Contents of Variable
by Marshall (Canon) on Oct 07, 2013 at 23:37 UTC
    I've seen the other Monks post's on this. I can't see how to replicate your problem either.

    I would suggest that you get to the basics. Forget this file I/O stuff and just get to some simple example of the substitution involved. That may help you figure this out yourself. Of course the standard advice of "use strict; use warnings;" is often helpful.

Re: s/this/$that/ Gives Variable Name, Not Contents of Variable
by Laurent_R (Canon) on Oct 07, 2013 at 17:16 UTC

    What do you get if you hard code "stuff" in your substitution? Something like this:

    s/XXYY/stuff/g;