CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
inserting a text file into a perl script.

 



zipflip
Deleted

May 13, 2000, 10:57 AM

Post #1 of 13 (2624 views)
inserting a text file into a perl script. Can't Post

i would like to make some standalone html templates that my script could read and then print as content. that way i can easily manipulate the many different page layouts i need.

how do i get my script to read a specific file, and then print its contents? or assign a scalar with the contents of the file (if that's possible).

thanks!!!


Cure
User

May 12, 2000, 10:09 PM

Post #2 of 13 (2624 views)
Re: inserting a text file into a perl script. [In reply to] Can't Post

Hi


Using a LIST context

open(DB, "</path/to/file/template.html") or die $!;
print <DB>;
close DB;

Using an scalar context to get the entire html file in a variable.

{
local $/;
open(DB, "</path/to/file/template.html") or die $!;
$cure = <DB>;
close DB;
}

So $cure value will be the entire html file.

$cure = @all;
Whenever ever you set an ARRAY equal to a scalar you will get the number of elements in the array(thats all)
example:
@all = qw(one two three four five six);
$cure = @all;
the value of $cure is 5 becuase there five elemts in the array.


Remember that @cure is not the same thing as "@cure". "@cure" is special in perl because of the parenthesis around the array.

example:

@zipfile = qw(one two three four five);

$scalar = "@zipfile";

the value of $scalar will be one two three four five.

so, when you do this"@zipfile" what your really doing is this--> $scalar = join($",@zipfile); but perl does that for you when you put an array in double qoutes.


Now, lets say you want to read in a database line by line, then you could do this-->


open(DB, "</path/to/file/template.html") or die $!;
push @cure,$_ while (<DB> );
close DB;

this will read your database line-by-line into an array..


Lets say you just wanted to find out how many lines are in the database--> do this-->

{
local $/;

open(DB, "</path/to/file/template.htmll") or die $!;
$cure = <DB>;

$lines =( $cure=~tr/\n/\n/);

the value of $lines will be the number of lines in your database;
close DB;
}


Cure




perlkid
stranger

May 12, 2000, 10:45 PM

Post #3 of 13 (2624 views)
Re: inserting a text file into a perl script. [In reply to] Can't Post

 Cure is right but as for what I have to say,

I don't know how to split a page yet. I would like to know and I even asked how to do somthing similar using lwp in the intermediate forum. So what I do is just make two separate files, header and footer, and use them util I learn how to split a page using tags.

I think that you can replace $value with a subroutine and it would work, but It might replace the tag literaly with &sub;

perlkid


[This message has been edited by perlkid (edited 05-13-2000).]


perlkid
stranger

May 13, 2000, 1:59 AM

Post #4 of 13 (2624 views)
Re: inserting a text file into a perl script. [In reply to] Can't Post

 
This will work for you,

open(DB, "</path/to/file/template.htm")
@all=<DB>;
$file="@all";

print "$file";

You could even put tags into the html and subtitute them with values that the perl generates like so,


open(DB, "</path/to/file/template.htm")
@all=<DB>;
$file="@all";
$file=~s/%tag%/$value/g;
print "$file";


That's It, Good Night.

perlkid


zipflip
Deleted

May 13, 2000, 7:54 AM

Post #5 of 13 (2624 views)
Re: inserting a text file into a perl script. [In reply to] Can't Post

hey thanks perlkid! looks simple enough!

about substituting tags---that sounds great! is there any way i can use tags to insert subroutines? or to use another tag to split a page into two scalars (e.g. $header and $footer)?

that would be superduper...


Cure
User

May 13, 2000, 11:20 AM

Post #6 of 13 (2624 views)
Re: inserting a text file into a perl script. [In reply to] Can't Post

Hi

Lets say you want to split the first half of your database--> just read it into a scalar, and then $half1 = substr($string,0,length($string)/2); thats it.

example:

{
local $/;
open(DATA, "cure.txt") or die $!;
$cure = <DATA>;
close DATA;
}

$half = substr($cure,0,length($cure)/2); # first half


and if you want to split the second half of the database you just do this:

{
local $/;
open(DATA, "cure.txt") or die $!;
$cure = <DATA>;
close DATA;
}

$half = substr($cure,length($cure)/2); # second half


Cure



zipflip
Deleted

May 13, 2000, 6:50 PM

Post #7 of 13 (2624 views)
Re: inserting a text file into a perl script. [In reply to] Can't Post

thank you both for teaching me so much... i dig it.

about splitting a file into $header and $footer--what if those aren't the same length? cure, i might be wrong, but your solution just snips it right down the middle no matter what, right?

what if the html had a special string within it like __SPLIT__ that the script could recognize so it would split the file at that point? so your html could be:

<html>
<head>
</head>
<body>
this is the header!
__SPLIT__
this is the footer!
</body>
</html>

and the script would maybe go:

open(TEMPLATE, "template.txt") or die $!;
until ($_ eq "__SPLIT__") # don't know if this is right. i'm trying to say until you get to the line with
# __SPLIT__
{
push @header,$_ while (<DB> );
}
$header = "@header";
close TEMPLATE;


open(TEMPLATE, "template.txt") or die $!;
@footer=<TEMPLATE>;
until ($_ eq "__SPLIT__") # don't know if this is right. i'm trying to say until you get to the line with
# __SPLIT__
{
$get_rid_of_footer = shift (@footer);
}
$get_rid_of_split_string = shift (@footer);
$footer = "@footer";
close TEMPLATE;

i'm not sure--would that work? (i am really wingin it on this one)

zip

[This message has been edited by zipflip (edited 05-13-2000).]

[This message has been edited by zipflip (edited 05-13-2000).]


perlkid
stranger

May 14, 2000, 2:05 AM

Post #8 of 13 (2624 views)
Re: inserting a text file into a perl script. [In reply to] Can't Post

 
I don't know this one so Cure will have to help you.

I can only suggest that you experiment with the split function. Like,

open(DB, "</yada/yada/yada.html");
@all=<DB>;
close(DB);
foreach $line (@all){
($head, $foot) =~ split(_SPLIT_, $line);
$header = "$head";
$footer = "$foot";
}

print "$header";
print "$footer";

Experiment!

So was I on the right track Cure?

perlkid


Cure
User

May 14, 2000, 8:46 AM

Post #9 of 13 (2624 views)
Re: inserting a text file into a perl script. [In reply to] Can't Post

Hi

One way to do it:-->
{
local $/;
open(DATA,"database.txt") or die $!;
$cure=<DATA>;
close(DATA);
}

($header, $footer) = split(/__SPLIT__/, $cure,2);

print $header;
print $footer;


heres another way to do it-->
{
local $/;
open(DATA,"database.txt") or die $!;
$cure=<DATA>;
close(DATA);
}

$header = substr($cure,0,index($cure,"_"));
$footer = substr($cure,index($cure,"__",)),$footer=~s/__SPLIT__//;

print $header;
print $footer;


Cure


zipflip
Deleted

May 17, 2000, 8:33 AM

Post #10 of 13 (2624 views)
Re: inserting a text file into a perl script. [In reply to] Can't Post

ok this works great to insert text into a perl script:

sub retrieve_content
{
local $/;
open(CONTENT, "<$content_folder_path/$form_data{'content_file'}") or die $!;
$content = <CONTENT>;
close CONTENT;
print $content;
}

however, i am not sure how to print it so it doesn't put in additional backslashes or other characters. for instance, if the text file contains an email address, it gets printed as my\@email.com instead of my@email.com. what is the best way to print it *exactly* as it appears?

thanks --zip


perlkid
stranger

May 17, 2000, 11:28 AM

Post #11 of 13 (2624 views)
Re: inserting a text file into a perl script. [In reply to] Can't Post

 
Try Doing Somthing Like This,

$content =~ s/\\@/\@/g;

This should substitute all \@ to @.

perlkid


zipflip
Deleted

May 17, 2000, 12:22 PM

Post #12 of 13 (2624 views)
Re: inserting a text file into a perl script. [In reply to] Can't Post

hey thanks perlkid!

actually i know that would work for this example, but i'm worried about what will happen for other special characters. (what if i had a $ in there orsomething) unfortunately i don't know enough about perl to predict *all* changes that perl will make to my text file. (and even if i did it's pain in the ass to write a regex for *each* situation) what i need is something that will ensure me that *every* character is printed *exactly* as it appears in the original text file.

if this isn't possible, i'll insert that regex, but i'm hoping there's a more comprehensive solution...

thanks!!!!!

zip


perlkid
stranger

May 18, 2000, 3:16 AM

Post #13 of 13 (2624 views)
Re: inserting a text file into a perl script. [In reply to] Can't Post

 
You can substitute a large variety of different characters with a single choice such as "@" in my last post, or nothing. Like so;

$content =~ s/\\@\\$\\%\\&\\*//g

This looks for "\@ \$ \% \& \*" and substitutes each finding with nothing, so it just takes them out. So if you wanted to substitute \$ with $ and \@with @, you would not be able to do that in the same regex, you would have to make several lines.

$content =~ s/\\@/\@/g
$content =~ s/\\$/\$/g
$content =~ s/\\%/\%/g
$content =~ s/\\&/\*/g
$content =~ s/\\*/\*/g

This would take all \$ and sub. them with $ and the same with \@, \%, \&, and \*.

Do You Get It?

If you just want to get rid of crap do this,

$content =~ s/\~\`\#\^\*\(\)\_\-\=\+\{\}\[\]\;\:\'\"\,\<\.\>\/\?//g;

You See?

Good Luck with it.

perlkid

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives