Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

RE: Something is wrong and I don'tkow what

by monk2b (Pilgrim)
on Oct 18, 2000 at 05:43 UTC ( #37264=note: print w/replies, xml ) Need Help??

in reply to Something is wrong and I don'tkow what

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
RE: RE: Something is wrong and I don'tkow what
by extremely (Priest) on Oct 18, 2000 at 06:53 UTC

    And now for another episode of HereDoc theater...

    Look at this for a second and see if you get it, eventually you will want to look at perldoc perlop and search down to Regexp Quote-Like Operators or look in the Camel 2ed at page 43 "Here Docs" (I've never found here-documents in perldoc, anyone??? ahh perldoc perldata, thanks tilly!)

    print <<BODY; Content-type: text/html <html><head><title>Nocc Call Log</title></head> <body bgcolor="#FFFFFF"> <h1>Nocc Call Log</h1> <table border="4"> <form action="post.cgi" method="POST"> <tr> <td> <h3>Callers Name: <input type="text" name="caller" size="25"></h3> </td> <td bgcolor="#FFDEAD"> <h3>Auto Time : <input TYPE="radio" NAME="time-Type" VALUE="auto_ +time"></h3> <br> <h3>Manual Time : <input TYPE="radio" NAME="time-Type" VALUE="manua +l_time"></h3> </td> <td bgcolor="#FFDEAD"> <h3>Time: <input type=\"text\" name=\"time\"></h3>\n"; </td>\n"; </tr>\n"; BODY # BODY is the end of the heredoc, and CANNOT have space # before or after it unless you get fancy # and do the print above as: # print <<" FANCY WITH Spaces in FRONT"; # and then you would end it with the following, minus the "#": # FANCY WITH Spaces in FRONT

    $you = new YOU;
    honk() if $you->love(perl)

      Something people tend to forget. It's not really a good idea to use:

      print <<BLOCK_END;

      Why? It's a bareword!

      Barewords aren't really defined in the language and can be subject to change. However:

      print <<"BLOCK_END"; will always expand variables while:

      print <<'BLOCK_END'; will perform no parsing of the text.

        In this case it most definitely is not a problem, indeed in the examples of "here-doc" syntax in perldata the first example uses a bare-word.

        While it may confuse people, the following does not confuse Perl:

        print <<print; Just another Perl hacker, print
        As tilly points out, perl has no problem with it. I tend to guide people towards the quoted version simply for a stylistic reason: not only does it look better (IMO) but it makes it easier to catch mistakes like:
        print << RAGNAROCK Thor Odin RAGNAROCK
        Technically, it isn't a bareword, AFAIK. -w doesn't complain about it, nor does use strict. It isn't a bareword, it's part of an operative phrase. You can even use perlfunc words and symbols there, try out:
        #!/usr/bin/perl -w use strict; print <<print; print me print (my $s = <<s) =~s/\//\/\//s; s\/\/\/\/s s print "$s";

        $you = new YOU;
        honk() if $you->love(perl)

RE: RE: Something is wrong and I don'tkow what
by Anonymous Monk on Oct 18, 2000 at 06:50 UTC
    No, you did NOT need alot of backslashes. But you DID fail to listen to the advice given.
      here here... i am afraid, monk2b that i have to back up what the Anonymous monk said. The insane amount of backslashes that you used, coupled with the multiple print statements are going to make your program slower *AND* unmaintainable.

      I don't know the slower, i am not 100% sure, and i have to get a functional spec done or i'd use Benchmark; it makes sense to me, that every time you call a print statement it has to make a function call of some sort (or builtin, or inline replacement, i don't know how the perl interpreter does it...) and that has a great deal of overhead. In your case, constructing the string in memory with HERE text, or using the qq operator (as suggested) by runring causes you to break up lines in places that algorithmically do not make sense (why print out each line individually, when you are trying to print out a document?)

      more worrisome to me however, is the maintainability of said code. Having worked for *way* too long on CGI/Web style code, I can guarantee you that maintainability is oft overlooked (even by code that i *know* will only be seen by me) and it's inexistance always regretted. By using so many \"'s and other assorted baddies, you are asking for an interpolation nightmare, if you can avoid having to enter *that* many \ characters in by hand, why on earth aren't you doing it, thereby making your code easier on the eyes, and easier on your wrists! Do yourself a favor, from now on, use the right operator for the right job, if it makes sense for some reason to print line by line by line, then do so, if however you are just trying to print a block of HTML and interpolate variables in there, Perl, the swiss army chainsaw of happyness that it is, has some really nice mechanisms for you to do it.

      UPDATE: yeah :) i had a few spelling errors... thanks to those who pointed them out (you know who you are turnstep :)... 12:46 a.m., still working on requirements spec... god help me.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://37264]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2022-05-22 01:37 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (78 votes). Check out past polls.