in reply to file contents as a part of code?

The simple, but presumably wrong, answer is that the file contents can just be slurped into a string and the string passed to the subroutine. Why is that the wrong answer?

open infile, "< datafile"; my $str = join "", <infile>; close infile; gnuplot (..., $str);

Perl is Huffman encoded by design.

Replies are listed 'Best First'.
Re^2: file contents as a part of code?
by davidrw (Prior) on Aug 17, 2005 at 02:04 UTC
    I think that OP want's the contents of the file eval'd .. so, to OP, i would say start with something live the above and simply perldoc -f eval it. Be aware, however, that this can be very dangerous--for example if someone puts malevalent perl code in the file it _will_ get executed.
    open FILEHANDLE, '<', '/tmp/MY_DATA' or die $!; my $str = do { local $/; <FILEHANDLE> }; close FILEHANDLE; gnuplot({ "title" => "My Trend", "output type" => "png", "output file" => "$TMPDIR/trend-$$.png"}, eval($str), );
    (Note: For slurp explaination, see Perl Idioms Explained - my $string = do { local $/; <FILEHANDLE> };)
      You teach one 'do' idiom, but then you skip the other power of 'do' and go the long way around.
      die if not -f '/tmp/MY_DATA'; gnuplot({ "title" => "My Trend", "output type" => "png", "output file" => "$TMPDIR/trend-$$.png"}, do '/tmp/MY_DATA' );
      (Not that this is an endorsement of either approach.)

      --
      [ e d @ h a l l e y . c c ]

        This worked like a charm! You guys are geniuses! Thanks for all your suggestions, you guys may have saved the few hairs on my head that I have not pulled out yet ;) Thanks again!!! Dave