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

Hi All,
This is probably a simple problem. So i'm trying to write something to a CSV file but I want to replace part of the file with a string stored in a variable. When I tried the code below it printed the variable name i.e $test rather than the string "functionTest". I also tried inserting double quotes but no luck. Any ideas? Thanks in advance!

use strict;
my $test = "functionTest";
open (MYFILE, '>>Z:\My Documents\Workspace\$test.csv');
print MYFILE "app1,stuff,stuff,stuff\n";

Replies are listed 'Best First'.
Re: Expanding a variable
by moritz (Cardinal) on Sep 09, 2010 at 13:55 UTC
    Use double quotes, but also double backslashes, ie "Z:\\My Documents\\Workspace\\$test.csv"

    See also: perlsyn.

    Somtimes use warnings is helpful here.

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: Expanding a variable
by SuicideJunkie (Vicar) on Sep 09, 2010 at 13:59 UTC

    You're using a single quoted string. Those don't interpolate. Adding double quotes inside makes literal double quotes.

    You should either convert the whole string to using double quotes (or equivalent) and escape your backslashes... or put the variable outside the quotes and concatenate it

    PS: any reason why you're not using a three arg open and lexical filehandles? Its a good habit to get into.

Re: Expanding a variable
by LanX (Saint) on Sep 09, 2010 at 13:58 UTC
     open (MYFILE, ">>Z:\My Documents\Workspace\$test.csv");

    variable interpolation only occurs within doublequotes.

    But careful, this example will not work, since you have to take care about escaping characters like backslashes!

    UPDATE: IMHO the best solution is an explicit concat:

     open (MYFILE, '>>', 'Z:\My Documents\Workspace\' . "$test.csv" );

    or

     open (MYFILE, '>>', 'Z:\My Documents\Workspace\' . $test . '.csv' );

    (untested)

    Cheers Rolf

Re: Expanding a variable
by Utilitarian (Vicar) on Sep 09, 2010 at 13:58 UTC
    I doubt you tried using double quotes, if you had your script would have opened a file of the right name, perhaps you mis-understood someone's instruction, the open command should read
    open (MYFILE, '>>', "Z:/My Documents/Workspace/$test.csv");
    The three parameter version, while not strictly needed in this instance is nicer as it avoids clobbering files if the string ends up starting with a > by mistake.

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
      Have you tested your code?
      $ perl -E 'my $x = 3; say "foo\$x"' foo$x

      (Agreed on the thre-argument open though)

      Perl 6 - links to (nearly) everything that is Perl 6.
        Edited to fix, no windows sys handy, so I forgot the obvious error.
        print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: Expanding a variable
by PerlScholar (Acolyte) on Sep 09, 2010 at 14:11 UTC
    Thanks everyone i've fixed it now. Will stick to three parameter version in future.