variable interpolation and file read

Discussion in 'Perl Misc' started by jkatz@artsci.wustl.edu, Nov 30, 2005.

  1. Guest

    Can I force variable interpolation on a file read?
    I'd like the two print statements below to produce the same
    results.....
    (where the file myinputfile just contains the exact characters in $line
    below--
    without the quotes of course. )


    #!/usr/bin/perl -w


    $myvalue="girl";
    $line= "It's a $myvalue ";
    print $line."\n";

    open (INPUT, "<myinputfile");
    $line=<INPUT>;
    print $line."\n";

    Alternatively, how could I use a s/// to make the substitution?
    (I want the substitution to work automatically for ANY variable I might
    have
    set earlier in the perl file.)

    Thanks in advance for your help!

    LCK
     
    , Nov 30, 2005
    #1
    1. Advertising

  2. Guest

    In fact efficency isn't a big issue here--the input files aren't very
    big. However the contents of $line can't be known by the perl script
    beforehand, only the names and values of the variables.
    I think however that what you suggests implies that I should read
    $line,
    split on either side of $myvalue and then eval
    $myvalue. Am I understanding you correctly?

    LCK
     
    , Nov 30, 2005
    #2
    1. Advertising

  3. "" <> writes:

    > In fact efficency isn't a big issue here--the input files aren't very
    > big. However the contents of $line can't be known by the perl script
    > beforehand, only the names and values of the variables.


    Reading between the lines a bit, it sounds like what you're looking for is
    a "template" library. These work sort of like the old "mad-lib" games. You
    create a template with placeholders in it, and then the template library
    reads that and fills in the proper values.

    There are many such libraries available in CPAN- search for "template" at
    <http://search.cpan.org>.

    My own favorite is the venerable Text::Template, although that's largely a
    matter of intertia on my part. I tried it out early on (I think it was the
    first such module on CPAN), and it's never pissed me off enough that I've
    felt the need to look at alternatives.

    > I think however that what you suggests implies that I should read
    > $line, split on either side of $myvalue and then eval
    > $myvalue. Am I understanding you correctly?


    A word of advice: Before you consider taking PG's advice on *anything*, you
    might want to review her posting history here.

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
     
    Sherm Pendley, Nov 30, 2005
    #3
  4. wrote:
    > Can I force variable interpolation on a file read?


    No.

    See FAQ: "How can I expand variables in text strings?"

    However the FAQ answer is incomplete so see also numerous[1] previous
    threads on the subject by seaching for the phrase "How can I expand
    variables in text strings?"

    [1] ~2/month for the last few years.
     
    Brian McCauley, Dec 1, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. \Rob\
    Replies:
    8
    Views:
    653
    Daniel Kirsch
    Feb 24, 2006
  2. Andrew Fabbro

    Variable interpolation question

    Andrew Fabbro, Nov 17, 2003, in forum: Python
    Replies:
    3
    Views:
    293
    anton muhin
    Nov 17, 2003
  3. Oliver Heins
    Replies:
    2
    Views:
    125
    Oliver Heins
    Sep 11, 2006
  4. phrankster

    Variable Interpolation with %%variable

    phrankster, Aug 9, 2005, in forum: Perl Misc
    Replies:
    9
    Views:
    148
    James Taylor
    Aug 14, 2005
  5. Josef Moellers
    Replies:
    8
    Views:
    85
    Florian Kaufmann
    Jan 23, 2008
Loading...

Share This Page