Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Returning Variables from files

by Anonymous Monk
on Jul 21, 2002 at 20:31 UTC ( #183867=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Ok say I two files called, header and footer, but all over header and footer it has variables, but how can I tanslate flatfile variables to perl, for example a file
--User.htm--- <html> <head> <title>$title</title> </head> <body $bodystuff> <font $face> $other </font> </body> </html>
I dont want it to print $other I want it to print the scripts $other

Replies are listed 'Best First'.
Re: Returning Variables from files
by dpuu (Chaplain) on Jul 21, 2002 at 20:49 UTC
    Imagine you wanted to write the output as a perl print statement: you could write:
    my $name = "Dave"; my $user = "dpuu"; print <<HERE; Hello, my name is $name and my user name is $user HERE
    This will do variable interpolation on a set of lines.

    If you want to separate the script and the flat-file, you can create a print-statement on the fly, using eval:

    my $name="Dave"; my $user="dpuu"; my $text_file="User.htm"; my $text=`cat $text_file`; eval "print <<HERE;\n$text\nHERE";
    You want to be careful when putting this in a production environment: executing data files as code could introduce some security holes (or just very strange bugs). This script won't work with perl's -T option --Dave.
Re: Returning Variables from files
by dpuu (Chaplain) on Jul 21, 2002 at 21:03 UTC
    Here's another possibility: its not as powerful as the eval method; but much safer:
    my %values = (title => "A Title"); sub get_value { my $key = shift; return exists($values{$key}) ? $values{$key} : "UNKNOWN"; } my $file = "User.htm"; open IN, "<$file" or die "can't read file: $file"; while (<IN>) { s/\$(\w+)/get_value($1)/ge; print; }
    You can obviously get a bit more fancy with the get_value subroutine --Dave.
      what if I wanted to do subroutines
Re: Returning Variables from files
by dorko (Prior) on Jul 22, 2002 at 02:28 UTC
    Maybe I'm missing something here, but you might want to consider using a templating system. They do what you seem to want to do here. (i.e. Have a place holder in an HTML file that gets replaced dynamically by your script.)

    I use HTML::Template. There are at least two good tutorials for HTML::Template available here at the Monastery. One is by Ovid and the other by jeffa.

    In all fairness, there are other templating modules out there. You can find an article ("Choosing a Templating System") comparing and contrasting them.

    Cheers!

    Brent

    -- Yeah, I'm a Delt.

Re: Returning Variables from files
by cjf (Parson) on Jul 21, 2002 at 21:01 UTC

    What format is the data in?

    Assuming var_name=var_value format, and only working with scalars, try the following:

    open HEADER, "header.txt" or die $!; my %variables; while (<HEADER>) { chomp; my ($name, $value) = split /=/; $variables{$name} = $value; } close HEADER; print <<EOF; <html> <head> <title>$variables{title}</title> </head> <body> <p>Hello $variables{name}</p> </body> </html> EOF

    That should work if each name/value pair is separated by a newline.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://183867]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2021-11-27 06:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?