Sounds like what you're trying to do is use one script to generate your data and another to plot it, and you want to be able to save your data to a file in-between. Is that it? If you're manually generating a "Perl-format" data structure and dumping that to a file, you're probably better off using various CPAN modules that help you serialize a Perl data structure and save it to a file. For example, Data::Dumper (and variants like Data::Dump) or YAML. Let's consider an example with YAML;

use YAML qw( DumpFile ); my @data = ( [ { "title" => "line 1", "style" => "lines" , "type" => "matrix" }, [ 1, 6318 ], [ 2, 7903 ], [ 3, 25617 ], ], [ { "title" => "line 2", "style" => "lines" , "type" => "matrix" }, [ 1, 6215 ], [ 2, 7831 ], [ 3, 25782 ], ], ); DumpFile( "output.txt", @data )

This produces an output file that looks like this:

--- #YAML:1.0 - style: lines title: line 1 type: matrix - - 1 - 6318 - - 2 - 7903 - - 3 - 25617 --- #YAML:1.0 - style: lines title: line 2 type: matrix - - 1 - 6215 - - 2 - 7831 - - 3 - 25782

Loading it back in from inside your other file is just the reverse, and then you can use it however you want:

use YAML qw( LoadFile ); my @data = LoadFile( "output.txt" ); gnuplot({ "title" => "My Trend", "output type" => "png", "output file" => "$TMPDIR/trend-$$.png"}, @data );

One advantage of YAML is that it's not Perl code, so you don't run the same "danger" in loading Perl code from a file and running "eval" or just running "do" on the file. (If it's just you using it, it's not much of a risk, but it's a bad habit to get into.)

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.


In reply to Re: file contents as a part of code? by xdg
in thread file contents as a part of code? by djwaddel

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.