Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > print <<webpage

Reply
Thread Tools

print <<webpage

 
 
phillyfan
Guest
Posts: n/a
 
      01-06-2006
I have a script where I use the print << web_page code. I am calling
functions within the print <<web_page and web_page lines.
Here is a snippet of my code:

div class=form>
Fill out the fields below and click Submit to update a proposal.<P>
<form name=prpslupd
action=\"http://$servername/cgi-bin/marketing/n/prpslupd\" method=post>
<table border=1>
<tr>
<td colspan=4>
<B>PROPOSAL INFORMATION </B>
</td>
</tr>
<tr>
<td>
Proposal<BR>Number:
</td>
<td>
$proposal[0] # this code is fine
</td>
</tr>
<tr>
<td>
Rep<BR>Number:
</td>
<td>
$proposal[2] # this code is fine
</td>
</tr>
<tr>
<td>
Proposal<BR>Type:
</td>
<td>
pop_list("proptype", $proposal[10]); # this is where the hangup is it
shows this instead of perfoming the function
</td>
</tr>
<tr>
<td>
Prospect:
</td>
<td colspan=3>
my $prspctlist=$dbh->prepare("select descr from CARE.CARE_PROSPECT
where pnum=$proposal[1]"); # this is where the hangup is it shows this
instead of perfoming the function
$prspctlist->execute(); # this is where the hangup is it shows this
instead of perfoming the function
my $descr=$prspctlist->fetchrow_array(); # this is where the hangup is
it shows this instead of perfoming the function
$descr
</td>
</tr>
<tr>
<td height=10>
</td>
</tr>
<tr>
<td>
Proposal<BR>Date:
</td>
<td>
pop_date("off", $proposal[3]); # this is where the hangup is it shows
this instead of perfoming the function


When looking at the webpage created, the function code shows, example
-- pop_date("off", $proposal[3]); shows instead of the information
the function should show. I would rather use the print << web_page
code instead of print for every line. Please advise.

Thank you in advance.

 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      01-07-2006
phillyfan wrote:
> I have a script where I use the print << web_page code. I am calling
> functions within the print <<web_page and web_page lines.


perldoc -q "function calls"

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      01-07-2006
phillyfan <> wrote:

> I have a script where I use the print << web_page code.



The "<<" is part of what is called a "here document", documented
in perlop.pod.

A here document is nothing more than an alternative way
of quoting strings.


> I am calling
> functions within the print <<web_page and web_page lines.


> pop_list("proptype", $proposal[10]); # this is where the hangup is it
> shows this instead of perfoming the function



The same as if you had used some other form for your quoting:

print qq(pop_list("proptype", $proposal[10]));


> When looking at the webpage created, the function code shows, example
> -- pop_date("off", $proposal[3]);



Your example does not match your code...


> shows instead of the information
> the function should show.



Scalars and arrays are interpolated in double-quotish strings.

Function calls are not interpolated.


> I would rather use the print << web_page
> code instead of print for every line. Please advise.



perldoc -q string

How do I expand function calls in a string?


But that "solution" is too ugly to look upon, so I advise putting
the result of the function call into a variable that will interpolate:

my $popped_date = pop_list("proptype", $proposal[10]);

print <<STUFF;
...
$popped_date
...
STUFF


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
phillyfan
Guest
Posts: n/a
 
      01-29-2006
Thanks you for your replies I was not able to get the code to work as
stated but ended up figuring how to make it work. I am now stuck with
the following issue. How do I get the the array information passed
into the array in the here documnent?

#!/usr/bin/perl -w

use strict;
use POSIX;
use CGI qw(:all);
use Fcntl qw(:flock);
use DBI;

#Globals
my $servername = "scdadev.angelica.com";
my $cgi = new CGI;
my $dbuser = "pda";
my $dbpasswd = "pdaadmin";
my $dbh = DBI->connect('dbi:Oracle:dev', $dbuser, $dbpasswd,
{PrintError=>0, RaiseError=>0}) || die "Can't
connect to database $DBI::errstr";

#Main
# Assign the CGI params to variables
my $qrystrg=$cgi->param("opr");
my @Url = split(/\?/,$qrystrg);
my $opr = $Url[0];
my $mtype = substr(($Url[1]), 6);
my $oprqry=$dbh->prepare("select pltnum from pda_user where
opr='$opr'");
$oprqry->execute();
my $pltnum=$oprqry->fetchrow_array();

my $seriallist=$dbh->prepare("select meter, descr
from pda.pda_meter
where svcnum = 49

order by descr");
$seriallist->execute();
my @select = ();
while(my @rows=$seriallist->fetchrow_array)
{
push(@select,"$rows[0]","$rows[1]"); <-This is the informstion I would
like to capture and pass to the the section below in the here doc
}


print << "WEB_PAGE";
*snipped*

<script language="JavaScript">
document.write('<P>Please enter your '+meters+' Number:');
document.write('select name=pltnum>');
document.write('<option>');
while( @selected)
{
document.write('<option value=$rows[0]>$rows[1]'); <- This is where I
would like to populate the info from the array above to the array here
}
document.write('</select>');
}document.write('<P>Please enter your '+meters+' Reading:');
</script>
<input name="read" type="text" id="read" size="10" maxlength="10">
<div>
*snipped*
</form>
</body>
</html>
WEB_PAGE
exit (0);

Thank you in advance for your help.

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      01-29-2006
phillyfan wrote:
> Thanks you for your replies I was not able to get the code to work as
> stated but ended up figuring how to make it work. I am now stuck with
> the following issue.


Then you should have started a new thread with a proper subject line.

> How do I get the the array information passed
> into the array in the here documnent?


<snip>

> my @select = ();
> while(my @rows=$seriallist->fetchrow_array)
> {
> push(@select,"$rows[0]","$rows[1]"); <-This is the informstion I would
> like to capture and pass to the the section below in the here doc
> }
>
> print << "WEB_PAGE";
> *snipped*
>
> <script language="JavaScript">
> document.write('<P>Please enter your '+meters+' Number:');
> document.write('select name=pltnum>');
> document.write('<option>');
> while( @selected)
> {
> document.write('<option value=$rows[0]>$rows[1]'); <- This is where I
> would like to populate the info from the array above to the array here
> }
> document.write('</select>');
> }document.write('<P>Please enter your '+meters+' Reading:');
> </script>


You can't pass variables from a server side CGI script to client side
JavaScript. If there is no particular reason to use JavaScript, you'd
better generate the whole form with Perl.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
phillyfan
Guest
Posts: n/a
 
      01-29-2006
Ok I fixed it and took out JavaScript. I now have a subroutine :

sub popserial{
my $seriallist=$dbh->prepare("select meter, descr
from pda.pda_meter
where svcnum = 49

order by descr");
$seriallist->execute();
my @select = ();
while(my @rows=$seriallist->fetchrow_array)
{
push(@select, "<option value=$rows[0]>$rows[1]")
}
return(@select);
}

that i call

my @mypopulate = popserial(); <- right here
my $line = "";
my $meter = "";
my $meters = metertype();

print << "WEB_PAGE";
*snipped*
Below is where I use the information from the sub. As you can see what
I am trying to do is take the info from the sub to populate a drop down
box.

<form name="ucr">

<P>Please enter your $meters Number:
<select name=name>
<option>
while(@mypopulate)
{
<option value="$mypopulate[0]">"$mypopulate[1]"
}

</select>

I get :
P>Please enter your Electric Number:
<select name=name>
<option>
while(<option value=65>Electric Meter <option value=36>Gas 1 <option
value=80>Waste Water Meter <option value=13>Water 1 <option
value=21>Water 2)
{
<option value="<option value=65>Electric Meter">"<option value=36>Gas
1"
}

</select>

The while loop code is printed into the drop down box.

Thanks to everyones help I am closer to solving this I just need to
figure out how to keep the "while(@select" from interpolating into my
drop down box and have the drop down box only show what I am requesting
from the array. I appreciate the fact that you don't write the code for
me and point me in a direction.

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      01-29-2006
phillyfan wrote:
> Ok I fixed it and took out JavaScript. I now have a subroutine :
>
> sub popserial{
> my $seriallist=$dbh->prepare("select meter, descr
> from pda.pda_meter
> where svcnum = 49
>
> order by descr");
> $seriallist->execute();
> my @select = ();
> while(my @rows=$seriallist->fetchrow_array)
> {
> push(@select, "<option value=$rows[0]>$rows[1]")
> }
> return(@select);
> }
>
> that i call
>
> my @mypopulate = popserial(); <- right here
> my $line = "";
> my $meter = "";
> my $meters = metertype();
>
> print << "WEB_PAGE";
> *snipped*
> Below is where I use the information from the sub. As you can see what
> I am trying to do is take the info from the sub to populate a drop down
> box.
>
> <form name="ucr">
>
> <P>Please enter your $meters Number:
> <select name=name>
> <option>
> while(@mypopulate)
> {
> <option value="$mypopulate[0]">"$mypopulate[1]"
> }
>
> </select>
>
> I get :
> P>Please enter your Electric Number:
> <select name=name>
> <option>
> while(<option value=65>Electric Meter <option value=36>Gas 1 <option
> value=80>Waste Water Meter <option value=13>Water 1 <option
> value=21>Water 2)
> {
> <option value="<option value=65>Electric Meter">"<option value=36>Gas
> 1"
> }
>
> </select>
>
> The while loop code is printed into the drop down box.


Yes, why did you expect otherwise?

As Tad pointed out, a here document is similar to a quoted string, and
won't execute Perl code.

Let the subroutine generate the needed HTML, and return a scalar with
the so prepared string.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
phillyfan
Guest
Posts: n/a
 
      01-29-2006
My subroutine generates html code but how to "return" it as a scalar is
what is confusing me. I now figured out that returning it as an array
was pointless because a return in a subroutine only does scalar, which
I totally was ignorant to. I read some of your answer on the returning
a scalar of an array. Can you now give me anymore information on where
to look to perform what I am trying to do?

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      01-29-2006
phillyfan wrote:
> My subroutine generates html code but how to "return" it as a scalar is
> what is confusing me. I now figured out that returning it as an array
> was pointless because a return in a subroutine only does scalar, which
> I totally was ignorant to.


A subroutine returns a list of values.

perldoc perlsub

> I read some of your answer on the returning
> a scalar of an array. Can you now give me anymore information on where
> to look to perform what I am trying to do?


I was thinking of something along these lines:

sub popserial {
...
my $options;
while ( my @rows=$seriallist->fetchrow_array ) {
$options .= qq(<option value="$rows[0]">$rows[1]</option>\n);
}
return $options;
}

my $options = popserial();

print <<WEB_PAGE;
*snipped*

<select name="name">
$options
</select>

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
phillyfan
Guest
Posts: n/a
 
      01-29-2006
It works.
I must now go and find out about .= and using qq inside a
here document for my own benefit. Thank you again Mr. Hjalmarsson.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
print a vs print '%s' % a vs print '%f' a David Cournapeau Python 0 12-30-2008 03:19 AM
Problem - I want to print Current Output of Pdf file and should print once.I get print dialog box but it is not working keto Java 0 05-30-2007 11:27 AM
Can't print to print server =?Utf-8?B?cGVjaw==?= Wireless Networking 2 02-03-2005 02:41 PM
How to make use of "print to file" option by print =?Utf-8?B?enljZ3M=?= MCSE 1 01-04-2005 06:50 PM
Unlarging the print to print using PDF file to print Bun Mui Computer Support 3 09-13-2004 03:15 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57