Print substitution ?

Discussion in 'Perl Misc' started by Bob, Sep 30, 2005.

  1. Bob

    Bob Guest

    I am trying to read in the contents of a file (which contains psuedo
    variables) and then print the file's contents and have variable
    substitution occur.

    Example:

    [file contains this text: "Your name is $name"]

    open (HEADER, "< ../header.htm")
    my @headerLines = <HEADER>;
    my $name = "Freddy";
    print_html("@headerLines");

    When I do this, I get the contents of the file, but the variable does
    not get substituted. So the output looks like this:

    Your name is $name

    Suggestions ? This is actually part of a much larger file read with
    many more variables and I could really use substitution.

    Thanks,
    Bob, Sep 30, 2005
    #1
    1. Advertising

  2. Bob

    Guest

    Try something more like:

    my $name = 'Freddy';
    open(my $header, '<', '../header.htm') or die "Can't read
    '../header.htm': $!\n";
    for (<$header>) {
    s/(\$\w+)/$1/eg;
    print_html($_);
    }
    close($header);
    , Sep 30, 2005
    #2
    1. Advertising

  3. Bob

    Paul Lalli Guest

    Bob wrote:
    > I am trying to read in the contents of a file (which contains psuedo
    > variables) and then print the file's contents and have variable
    > substitution occur.


    Your Question is Asked Frequently
    perldoc -q expand
    How can I expand variables in text strings?

    Paul Lalli
    Paul Lalli, Sep 30, 2005
    #3
  4. Bob <> writes:

    > I am trying to read in the contents of a file (which contains psuedo
    > variables) and then print the file's contents and have variable
    > substitution occur.


    Search CPAN for the word "template" - this is very well-explored territory,
    so there's little need to reinvent that particular wheel.

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Sep 30, 2005
    #4
  5. Sherm Pendley <> writes:

    > Bob <> writes:
    >
    >> I am trying to read in the contents of a file (which contains psuedo
    >> variables) and then print the file's contents and have variable
    >> substitution occur.

    >
    > Search CPAN for the word "template" - this is very well-explored territory,
    > so there's little need to reinvent that particular wheel.


    Forgot the link:

    <http://search.cpan.org>

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Sep 30, 2005
    #5
  6. Bob

    John Bokma Guest

    Bob <> wrote:

    > I am trying to read in the contents of a file (which contains psuedo
    > variables) and then print the file's contents and have variable
    > substitution occur.
    >
    > Example:
    >
    > [file contains this text: "Your name is $name"]
    >
    > open (HEADER, "< ../header.htm")
    > my @headerLines = <HEADER>;
    > my $name = "Freddy";
    > print_html("@headerLines");
    >
    > When I do this, I get the contents of the file, but the variable does
    > not get substituted. So the output looks like this:
    >
    > Your name is $name
    >
    > Suggestions ? This is actually part of a much larger file read with
    > many more variables and I could really use substitution.


    my %vars = (

    name => "Freddy",
    :
    :
    );

    while ( my $line = <HEADER> )

    $line =~ s{\$(\w+)}{

    defined $vars{ $1 } ? $vars{ $1 } : 'ERROR'
    }ge;

    print $line;
    }

    (untested, use at your own risk, etc.)

    But as Sherm already explained: there are many template solutions @
    CPAN.

    Some tips though:

    check your open
    don't use camelCase (it's Perl, not Java)
    "@headerLines" probably doesn't do what you want.


    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    I ploink googlegroups.com :)
    John Bokma, Sep 30, 2005
    #6
  7. Paul Lalli wrote:

    > Bob wrote:
    >
    >>I am trying to read in the contents of a file (which contains psuedo
    >>variables) and then print the file's contents and have variable
    >>substitution occur.

    >
    > Your Question is Asked Frequently
    > perldoc -q expand
    > How can I expand variables in text strings?


    But is _not_ well answered in the FAQ. For details please review
    numerous previous threads containig the exact phrase "How can I expand
    variables in text strings?"

    For example.

    http://groups.google.com/group/comp...ebb1a8fcdae/88b9d342d1898a5d#88b9d342d1898a5d
    Brian McCauley, Sep 30, 2005
    #7
    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. Bad_Kid
    Replies:
    3
    Views:
    3,933
    Shawn H. Mesiatowsky
    Apr 7, 2005
  2. Bryan Shaw

    Lock Print Screen and File Print

    Bryan Shaw, Apr 19, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    3,931
    Kevin Spencer
    Apr 20, 2005
  3. EDom
    Replies:
    0
    Views:
    432
  4. keto
    Replies:
    0
    Views:
    897
  5. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    335
    David Cournapeau
    Dec 30, 2008
Loading...

Share This Page