print <<webpage

P

phillyfan

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.
 
G

Gunnar Hjalmarsson

phillyfan said:
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"
 
T

Tad McClellan

phillyfan said:
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
 
P

phillyfan

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.
 
G

Gunnar Hjalmarsson

phillyfan said:
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?

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.
 
P

phillyfan

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.
 
G

Gunnar Hjalmarsson

phillyfan said:
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.
 
P

phillyfan

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?
 
G

Gunnar Hjalmarsson

phillyfan said:
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>
 
P

phillyfan

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.
 
J

Jürgen Exner

phillyfan said:
My subroutine generates html code but how to "return" it as a scalar
is what is confusing me.

See "perldoc -f return"
I now figured out that returning it as an
array was pointless because a return in a subroutine only does
scalar,

Where did you find this piece of misinformation?
which I totally was ignorant to. I read some of your answer
on the returning a scalar of an array.

What is a "scalar of an array"? Did you mean a reference to an array?

jue
 
J

Jürgen Exner

phillyfan said:
It works.

What works? Please quote appropriate context when replying -as has been
customary for two decades- such that people have a chance to know what you
are refering to.
I must now go and find out about .= and using qq inside a
here document my own benefit.

Of course you can use those characters and character combination in a string
(regardless if the string is defined as a here document or with normal
quotes, single or double) but why do you think they are special? They are
just plain ordinary text pieces, just like any other character or character
combination (ignoring backslash escapes for the moment).

jue
 
A

Alan J. Flavell

Of course you can use those characters and character combination in
a string (regardless if the string is defined as a here document or
with normal quotes, single or double) but why do you think they are
special? They are just plain ordinary text pieces, just like any
other character or character combination (ignoring backslash escapes
for the moment).

You know - even for someone who *is* aware of the existence of the
Here-doc notation, it isn't exactly easy to find the documentation for
it. When I feel a need to consult it, I usually stumble around quite
a bit before remembering that it appears in perlop, under (quite some
way "under", in fact) the general heading of "Quote and Quote-like
Operators". (Online that could be
http://perldoc.perl.org/perlop.html#Quote-and-Quote-like-Operators ,
but of course there are copies in any complete Perl installation).

I don't know what to propose - I just wish there was some catchy
keyword, that one could commit to memory and that could be easily
searched for in the docs. A search for here-doc doesn't seem to be
useful, and a search for "here" or for <<EOF produces far too many
false matches.

cheers
 
J

Jürgen Exner

Alan said:
You know - even for someone who *is* aware of the existence of the
Here-doc notation, it isn't exactly easy to find the documentation for
it. [...]

I don't know what to propose - I just wish there was some catchy
keyword, that one could commit to memory and that could be easily
searched for in the docs. A search for here-doc doesn't seem to be
useful, and a search for "here" or for <<EOF produces far too many
false matches.

AMEN to that!!!

jue
 
G

Gunnar Hjalmarsson

phillyfan said:
I must now go and find out about .= and using qq inside a
here document for my own benefit.

They are operators without any useful meaning inside a here document,
and I didn't use them there either. Within a here document you can have
plain text, scalar variables and arrays. Other Perl code won't be
interpreted.

In "perldoc perlop" you can read up on both those operators as well as
on here documents.
Thank you again Mr. Hjalmarsson.

You are welcome.
 
D

David H. Adler

You know - even for someone who *is* aware of the existence of the
Here-doc notation, it isn't exactly easy to find the documentation for
it. When I feel a need to consult it, I usually stumble around quite
a bit before remembering that it appears in perlop, under (quite some
way "under", in fact) the general heading of "Quote and Quote-like
Operators".

Count your blessings. It used to be in perldata. :)

dha
 
P

phillyfan

They are operators without any useful meaning inside a here document,
and I didn't use them there either. Within a here document you can have
plain text, scalar variables and arrays. Other Perl code won't be
interpreted.
You are correct Mr. Hjalmarsson I apologize for misrepresenting how you
used the code. I am doing research into the .= syntax now.
Mr. Exner I was thanking who helped me with my problem so I figured
they would know what I meant by it works. If you followed this thread
it would have been evident.
You know - even for someone who *is* aware of the existence of the
Here-doc notation, it isn't exactly easy to find the documentation for
it. [...]
As Mr. Flavell states finding info on here-doc or heredoc as it is also
written sometimes is a like a treasure hunt. I know when asking for
help you are at the mercy of whoever answers but geez even I as a
person who knows more about somethings, other than Perl of course, I
try and answer, even what may be percieved as, the dumbest question in
a way a person will learn from my answer and ask again when they have
a question. I searched for information through postings before I ask
about problems.
Thank you all once again I now have more knowledge and insight into
Perl then when i woke up and at the end of the day isn't that what
matters?
 
I

Ian Wilson

phillyfan said:
Mr. Exner I was thanking who helped me with my problem so I figured
they would know what I meant by it works. If you followed this thread
it would have been evident.

Not necessarily. Don't forget: old newsgroup messages expire and are
deleted, new messages can arrive out of order (i.e. before the article
to which they are a comment).

Besides which, a lot of people read just the new messages without
re-reading the whole thread each time (its a lot faster that way).

http://groups.google.com/googlegroups/posting_style.html#summarize

http://www.catb.org/~esr/faqs/smart-questions.html

http://cfaj.freeshell.org/google/
 
G

Gunnar Hjalmarsson

Ian said:
Not necessarily. Don't forget: old newsgroup messages expire and are
deleted, new messages can arrive out of order (i.e. before the article
to which they are a comment).

That was most likely not the issue in this case.

IMO there was nothing obvious that could have been quoted to give
context in this particular case. The OP just let us know that the
problem was solved by help of previous replies in the thread.
Besides which, a lot of people read just the new messages without
re-reading the whole thread each time (its a lot faster that way).

If you post a follow-up without reviewing the thread, you break the
netiquette.
http://groups.google.com/group/comp.lang.perl.misc/browse_frm/thread/b9b4eeb7aabaf998
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top