Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Substitution in HTML page 1

Status
Not open for further replies.

JimJx

Technical User
Joined
Feb 16, 2001
Messages
202
Location
US
Hi again all,

I am trying to do a simple substitution in a page but the vars don't seem to be passing for some reason....

everything displays, but the sub is not done...

Basically, I am reading from a txt file and trying to grab the values from there and display the html page with the substitutions done.

The user is supposed to get a temp name and pass that displays, but not displaying. In the HTML page, I get nothing...

Any suggestions greatly appreciated....
Jim

Code:
open INF,'</var/[URL unfurl="true"]www/htdocs/cgi-bin/user.txt'[/URL] or die "Can't open input file";
my $flag = 0;
while (<INF>)
{
   local($^I, @ARGV) = ('.bak', 'user.txt');
   while (<>) {
      if (/^(.+)(\|No)$/ && $flag != 1) {
         $flag = 1;
         print "$1|Yes\n";
         ($Name,$Pass,$Used) = split /\|/,$_,3;
      }
      else {
         print;
      }
   }
}
close INF;

open (PAGE, "$newUser") or die ("Unable to open the file $newUser. $!");
     while (<PAGE>)                
     {
    	s/%%name%%/$Name/g;
	s/%%pass%%/$Pass/g;
    	print $_;
}
close (PAGE)

On the page I am using
Code:
Username: %%name%%<br />
Password: %%pass%%<br />
 
you may need to escape the percent signs as they are reserved for hashes.
Code:
s/\%\%name\%\%/$Name/g;
    s/\%\%pass\%\%/$Pass/g;




"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
also you don't seem to be applying the substitue to anything, unless i'm missing something?

that possibly should be
Code:
$_ =~ s/\%\%name\%\%/$Name/g;
$_ =~ s/\%\%pass\%\%/$Pass/g;


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
None of the suggestions worked......

The second code block is from the page I am trying to do the sub in....

 
the problem is you have opened the file for reading and then try and rewrite some of the lines inplace using a regexp:

Code:
open (PAGE, "$newUser") or die ("Unable to open the file $newUser. $!");
     while (<PAGE>)                
     {
        s/%%name%%/$Name/g;
    s/%%pass%%/$Pass/g;
        print $_;
}
close (PAGE)

you can't do that. You would have to use the inplace editor like your first block of code does with user.txt or write the file to an array or string and the edit the array or string and then overwrite the file.

Code:
open (PAGE, "$newUser") or die ("Unable to open the file$newUser. $!");
close (PAGE); 
my $text = do {local $/; <PAGE>};
$text =~ s/%%name%%/$Name/g;
$text=~  s/%%pass%%/$Pass/g;

open (PAGE, "$newUser") or die ("Unable to open the file $newUser. $!");
print PAGE $text;
close (PAGE);

1DMF,

the '%' symbol has no meta meaning inside a regexp so does not need to be escaped.

- Kevin, perl coder unexceptional!
 
oops, my code should be:

Code:
open (PAGE, "$newUser") or die ("Unable to open the file$newUser. $!");
my $text = do {local $/; <PAGE>};
[b]close (PAGE);[/b]
$text =~ s/%%name%%/$Name/g;
$text =~ s/%%pass%%/$Pass/g;

open (PAGE, "$newUser") or die ("Unable to open the file $newUser. $!");
print PAGE $text;
close (PAGE);

[3eyes] [purpleface] [wink]

- Kevin, perl coder unexceptional!
 
damn damn damn:

Code:
open (PAGE, "$newUser") or die ("Unable to open the file$newUser. $!");
my $text = do {local $/; <PAGE>};
close (PAGE);
$text =~ s/%%name%%/$Name/g;
$text =~ s/%%pass%%/$Pass/g;

open (PAGE, "[COLOR=red][b]>[/b][/color]$newUser") or die ("Unable to open the file $newUser. $!");
print PAGE $text;
close (PAGE);

I need a stiff drink!

- Kevin, perl coder unexceptional!
 
Hi Kevin,

Thanks for all of the help, but, when I use your code, for some reason I get a blank page....

Nothing is being written at all.....
 
I think I may have misunderstood what you are trying to do. If you are opening a text/html document and just need to make the subsitution to print the new data to the screen, and not the file, you do not have to do what I suggested. I was assuming you wanted to write the new data to the file. If you only need to write the new data to the screen and not the file this should work:

Code:
open (PAGE, "$newUser") or die ("Unable to open the file $newUser. $!");
while (<PAGE>) {
   s/%%name%%/$Name/g;
   s/%%pass%%/$Pass/g;
   print $_;
}
close (PAGE)

if that is being displayed in a web browser you have to print an HTTP header before printing any data to the screen. Has your script printed an HTTP header? Something like:

Code:
print "Content-type: text/html\n\n";

or if using the CGI module:

Code:
print header();

or:

Code:
print $q->header();

if you do want to print the data to the file then display it in the browser:

Code:
open (PAGE, "$newUser") or die ("Unable to open the file$newUser. $!");
my $text = do {local $/; <PAGE>};
close (PAGE);
$text =~ s/%%name%%/$Name/g;
$text =~ s/%%pass%%/$Pass/g;

open (PAGE, ">$newUser") or die ("Unable to open the file $newUser. $!");
print PAGE $text;
close (PAGE);

print $text;

once again, make sure your script has printed an HTTP header first before trying to do the last print line, which prints the variable to the screen/browser.

- Kevin, perl coder unexceptional!
 
JimJx,

I'm going to assume that STDOUT is the HTML page you are rendering to the browser...

With that in mind, assuming that you've already written the headers, and a tweak or so to Kevin's code:

Code:
open (PAGE, "$newUser") or die ("Unable to open the file$newUser. $!");
my $text = do {local $/; <PAGE>;};
close (PAGE);

$text =~ s/%%name%%/$Name/g;
$text =~ s/%%pass%%/$Pass/g;
print $text;
 
OK, ignore the man behind the curtain.. Kevin answered whilst I was composing.
 
and I thought I was a slow typist! [pc3]

Not to worry brigmar... It's all good (especially on Friday afternoon with some Margaritas waiting for me at the Cantina)! [2thumbsup]

- Kevin, perl coder unexceptional!
 
Thanks for taking the time to Kevin and Brigmar. I have used s/%%var%%/$var/g; before, that is why this has me stumped.... So, I went in and added a count....

Code:
open INF,"user.txt" or die "Can't open input file";
my $flag = 0;
[COLOR=#ff0000]my $count=1;[/color]
   while (<>) {
   [COLOR=#ff0000]$count++;[/color]
      my ($Name, $Pass, $Used) = split(/\|/);
      if (/^(.+)(\|No)$/ && $flag == 0) {
         $flag = 1;
         print "$1\|Yes\n";
      }
      else {
         print;
      }

}
      print "Name: $Name -- Pass : $Pass -- Count : $count\n";

open (PAGE, "$newUser") or die ("Unable to open the file newUser. $!");
     while (<PAGE>)                
     {
    	s/%%name%%/$Name/g;
		s/%%pass%%/$Pass/g;
    	print $_;
}
close (PAGE)

After everything executes, I get blanks for the first 2 vars, and 1 for the count. So, it seems to me like this is just not going all of the way through the file.

Any suggestions on how to test this or how to improve on what I have?

Thanks!
Jim
 
You have a scoping problem.

my ($Name, $Pass, $Used) = split(/\|/);

the above variables are only scoped for the 'while' loop that pulls them in from the user.txt file. You have to scope them so they are visible to all of the above code you posted. See if this helps:

Code:
open INF,"user.txt" or die "Can't open input file";
my $flag = 0;
[b]my ($Name, $Pass, $Used);[/b]
my $count=1;
   while (<INF>) {
   $count++;
      [b]($Name, $Pass, $Used) = split(/\|/);[/b]
      if (/^(.+)(\|No)$/ && $flag == 0) {
         $flag = 1;
         print "$1\|Yes\n";
      }
      else {
         print;
      }

}
      print "Name: $Name -- Pass : $Pass -- Count : $count\n";

open (PAGE, "$newUser") or die ("Unable to open the file newUser. $!");
     while (<PAGE>)                
     {
        s/%%name%%/$Name/g;
        s/%%pass%%/$Pass/g;
        print $_;
}
close (PAGE)

$Name and $Pass will have the value of the the last line in the user.txt file, which might not be what you want unless it's a one line file.

- Kevin, perl coder unexceptional!
 
on a side note guys, with this peice of code
Code:
     while (<PAGE>)                
     {
        s/%%name%%/$Name/g;
        s/%%pass%%/$Pass/g;
        print $_;
}

I take it that the substitute is auto applied to $_ , so where else does this apply? do all loops auto apply to $_ without you having to explicitly apply it?

Doesn't using this method cause that 'implicit use of blah blah not allowed' error?

when can you imply the use of a system var @_ , $_ etc.. and when must you explicitly use them?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top