print <<webpage

Discussion in 'Perl Misc' started by phillyfan, Jan 6, 2006.

  1. phillyfan

    phillyfan Guest

    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.
     
    phillyfan, Jan 6, 2006
    #1
    1. Advertising

  2. 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
     
    Gunnar Hjalmarsson, Jan 7, 2006
    #2
    1. Advertising

  3. 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
     
    Tad McClellan, Jan 7, 2006
    #3
  4. phillyfan

    phillyfan Guest

    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.
     
    phillyfan, Jan 29, 2006
    #4
  5. 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
     
    Gunnar Hjalmarsson, Jan 29, 2006
    #5
  6. phillyfan

    phillyfan Guest

    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.
     
    phillyfan, Jan 29, 2006
    #6
  7. 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
     
    Gunnar Hjalmarsson, Jan 29, 2006
    #7
  8. phillyfan

    phillyfan Guest

    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?
     
    phillyfan, Jan 29, 2006
    #8
  9. 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
     
    Gunnar Hjalmarsson, Jan 29, 2006
    #9
  10. phillyfan

    phillyfan Guest

    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.
     
    phillyfan, Jan 29, 2006
    #10
  11. phillyfan wrote:
    > 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ürgen Exner, Jan 29, 2006
    #11
  12. phillyfan wrote:
    > 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
     
    Jürgen Exner, Jan 29, 2006
    #12
  13. On Sun, 29 Jan 2006, Jürgen Exner wrote:

    > phillyfan wrote:
    >
    > > 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).


    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
     
    Alan J. Flavell, Jan 29, 2006
    #13
  14. Alan J. Flavell wrote:
    > 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
     
    Jürgen Exner, Jan 29, 2006
    #14
  15. phillyfan wrote:
    > 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.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jan 29, 2006
    #15
  16. On 2006-01-29, Alan J. Flavell <> wrote:
    >
    > 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

    --
    David H. Adler - <> - http://www.panix.com/~dha/
    And, considering the prevalence of displaced psychiatric patients
    among the ranks of the homeless, the viewpoints are usually guaranteed
    to involve aliens or 7 foot tall magical cats. - Mark Rogaski
     
    David H. Adler, Jan 30, 2006
    #16
  17. phillyfan

    phillyfan Guest

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

    Alan J. Flavell wrote:
    > 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?
     
    phillyfan, Jan 30, 2006
    #17
  18. phillyfan

    Ian Wilson Guest

    phillyfan wrote:

    > 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/
     
    Ian Wilson, Jan 30, 2006
    #18
  19. Ian Wilson wrote:
    > phillyfan wrote:
    >> 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).


    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

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jan 30, 2006
    #19
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    455
  2. keto
    Replies:
    0
    Views:
    1,023
  3. Paul
    Replies:
    14
    Views:
    886
    Alexey Smirnov
    Jun 19, 2008
  4. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    389
    David Cournapeau
    Dec 30, 2008
  5. sifar
    Replies:
    5
    Views:
    459
Loading...

Share This Page