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

Hi All,
Quick question on CSV files. I have the following code:

open (TEST, '>', "Z:/My Documents/Workspace/test.csv"); print TEST "File_1,link\n";

Instead of printing "link" in the second cell as above I would like to print Excels hyperlink formula. Which is of the format: =Hyperlink("c:\test\File_1.doc","Link")

Is there a way to put in that formula in place of link without messing up the syntax (and keeping the formula in one cell). Many thanks!

Replies are listed 'Best First'.
Re: Write to CSV file problem
by marto (Cardinal) on Sep 12, 2010 at 18:34 UTC
Re: Write to CSV file problem
by graff (Chancellor) on Sep 12, 2010 at 18:53 UTC
    Assuming that you really have some reason for wanting that sort of odd string in a flat, plain-text csv file, the Text::CSV module will "do the right thing" in terms of applying the necessary character escapes. You just need to make sure you get the intended characters into the string that you pass to your csv object (in the example below, I use the q// quote operator to simplify that part):
    use strict; use warnings; use Text::CSV; my @row = ( 'File_1', q{=Hyperlink("c:\test\File_1.doc","Link")} ); my $csv = Text::CSV->new(); { local $\ = "\015\012"; # or use the "eol" attribute of csv object open( my $csvfh, ">", "out.csv" ) or die $!; $csv->print( $csvfh, \@row ); }
Re: Write to CSV file problem
by Anonymous Monk on Sep 12, 2010 at 18:29 UTC