interpolation

Discussion in 'Perl Misc' started by new2perl@gmail.com, Jul 20, 2007.

  1. Guest

    Assigning a variable.
    Then reading in text file that uses the variable.
    It's isn't resolved.

    Input file:
    Your name is: $name

    Script:
    #!/bin/perl

    $name="Rupert";
    $text="textin";

    open (MYFILE, $text) or die "cant open erik";

    while (<MYFILE>) { $value = $_ }

    print "$value";


    Output:
    Your name is: $name


    Output that I want:
    Your name is Rupert.


    What am I doing wrong here ?

    Bob
     
    , Jul 20, 2007
    #1
    1. Advertising

  2. <> wrote:
    > Assigning a variable.
    > Then reading in text file that uses the variable.
    > It's isn't resolved.



    Of course not. Data is not the same thing as Code.


    > Input file:
    > Your name is: $name
    >
    > Script:
    > #!/bin/perl


    use strict;
    use warnings;


    > $name="Rupert";


    my $name="Rupert";


    > $text="textin";
    >
    > open (MYFILE, $text) or die "cant open erik";



    You did not try to open "erik", you tried to open "textin".


    > while (<MYFILE>) { $value = $_ }



    That reads all the lines, and discards all of them except the last line.


    > print "$value";



    print $value;

    perldoc -q vars

    What’s wrong with always quoting "$vars"?


    > Output:
    > Your name is: $name
    >
    >
    > Output that I want:
    > Your name is Rupert.
    >
    >
    > What am I doing wrong here ?



    Conflating what is "data" with what is "code".


    Your Question is Asked Frequently:

    perldoc -q expand

    How can I expand variables in text strings?


    However, I'm quite sure that you are asking the wrong question.

    You should instead be asking about Templating systems.


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad McClellan, Jul 20, 2007
    #2
    1. Advertising

  3. Guest

    On Jul 19, 7:32 pm, Tad McClellan <> wrote:
    > <> wrote:
    > > Assigning a variable.
    > > Then reading in text file that uses the variable.
    > > It's isn't resolved.

    >
    > Of course not. Data is not the same thing as Code.
    >
    > > Input file:
    > > Your name is: $name

    >
    > > Script:
    > > #!/bin/perl

    >
    > use strict;
    > use warnings;
    >
    > > $name="Rupert";

    >
    > my $name="Rupert";
    >
    > > $text="textin";

    >
    > > open (MYFILE, $text) or die "cant open erik";

    >
    > You did not try to open "erik", you tried to open "textin".
    >
    > > while (<MYFILE>) { $value = $_ }

    >
    > That reads all the lines, and discards all of them except the last line.
    >
    > > print "$value";

    >
    > print $value;
    >
    > perldoc -q vars
    >
    > What's wrong with always quoting "$vars"?
    >
    > > Output:
    > > Your name is: $name

    >
    > > Output that I want:
    > > Your name is Rupert.

    >
    > > What am I doing wrong here ?

    >
    > Conflating what is "data" with what is "code".
    >
    > Your Question is Asked Frequently:
    >
    > perldoc -q expand
    >
    > How can I expand variables in text strings?
    >
    > However, I'm quite sure that you are asking the wrong question.
    >
    > You should instead be asking about Templating systems.
    >
    > --
    > Tad McClellan
    > email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


    this should do it:
    $value =~ s/\$(\w+)/${$1}/g;
     
    , Jul 20, 2007
    #3
  4. Dr.Ruud Guest

    [OT] quoting UTF-8 in an iso-8859-1 context (was: Re: interpolation)

    Tad McClellan schreef:


    > Content-Type: text/plain; charset=iso-8859-1
    > [...]
    > Whatâ?Ts wrong


    Try LANG="" perldoc ...

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Jul 21, 2007
    #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. Replies:
    8
    Views:
    6,219
    Researcher_123
    Oct 7, 2011
  2. Mat W
    Replies:
    3
    Views:
    453
    Skeleton Man
    May 21, 2004
  3. \Rob\
    Replies:
    8
    Views:
    688
    Daniel Kirsch
    Feb 24, 2006
  4. Michele Puccini
    Replies:
    0
    Views:
    422
    Michele Puccini
    Nov 9, 2003
  5. Raj
    Replies:
    2
    Views:
    1,967
Loading...

Share This Page