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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

output from script 1

Status
Not open for further replies.

Headmaster

Technical User
Nov 27, 2002
79
US
Hi

I'm learning this CGI Perl stuff. Is their any way to get output from a script without using:

print header, start_html('test'),"text",end_html;

I have a line similar to that in my script. If I comment it out, the script 'blows up'. If it's left in, then it all seems to work fine.

Thanks for the advice!
Scott
 
In addition to the first post, I have print statements below this line. I'd like to use them without this line.
 
#!D:\win32app\Perl\bin\perl.exe
use CGI;
my $q = new CGI;
$myname = "Shelby";

print "Content-type: text/html\n\n";
print qq (
<html><body>
&quot;This is my HTML&quot;, $myname
</body></html>
);

 
I guess I need to rephrase my question...it was 5 minutes before quitting time when I typed the last one.

Here's the code I'm struggling with:

use LWP::UserAgent;
use HTTP::Request::Common qw(POST);
$ua = LWP::UserAgent->new;
$query->append('x_Response_Code','1');
my $req = POST ' [ map { $_ => $query->param($_) } $query->param() ];
print $ua->request($req)->as_string;

The test3 script's output is in html already, so when I return it, the headers print out in the browser as text. If I include
print &quot;Content-type: text/html\n\n&quot;;
or
print &quot;Content-type: text/plain\n\n&quot;;
it does the same thing (text/plain is plain ugly!).

The actual question is: how can I print the output from another script that is already formatted as html. That is the only output I need in the script. I'm still searching other sources, but if somebody has a solution...

Thanks for the replies!
 
$data = $ua->request($req)->as_string;

$final_data =~s/Content-type: text/plain\n\n/ /gi;

print $final_data;

This will simply substitute the HTTP header with a space.



haunter@battlestrata.com
 
OK Haunter, I understand what you did...Thanks for replying!

Only question I have is: What does the ~ do in the 2nd statement? My Perl book has no leads, except for the linux home directive. =)

Thanks again!
Scott
 
To capture the result of a substitution you use the tilda '~' operator.
This is the form

$newvariable =~s/somehing/somethingelse/;



This is how s///; works

s/PATTERN/REPLACEMENT/egimosx

Searches a string for a pattern, and if found, replaces that pattern with the replacement text and returns the number of substitutions made. Otherwise it returns false (specifically, the empty string).
If no string is specified via the =~ or !~ operator, the $_ variable is searched and modified. (The string specified with =~ must be scalar variable, an array element, a hash element, or an assignment to one of those, i.e., an lvalue.)

If the delimiter chosen is a single quote, no interpolation is done on either the PATTERN or the REPLACEMENT. Otherwise, if the PATTERN contains a $ that looks like a variable rather than an end-of-string test, the variable will be interpolated into the pattern at run-time. If you want the pattern compiled only once the first time the variable is interpolated, use the /o option. If the pattern evaluates to the empty string, the last successfully executed regular expression is used instead. See the perlre manpage for further explanation on these. See the perllocale manpage for discussion of additional considerations that apply when use locale is in effect.

Options are:

e Evaluate the right side as an expression.
g Replace globally, i.e., all occurrences.
i Do case-insensitive pattern matching.
m Treat string as multiple lines.
o Compile pattern only once.
s Treat string as single line.
x Use extended regular expressions.
Any non-alphanumeric, non-whitespace delimiter may replace the slashes. If single quotes are used, no interpretation is done on the replacement string (the /e modifier overrides this, however). Unlike Perl 4, Perl 5 treats backticks as normal delimiters; the replacement text is not evaluated as a command. If the PATTERN is delimited by bracketing quotes, the REPLACEMENT has its own pair of quotes, which may or may not be bracketing quotes, e.g., s(foo)(bar) or s<foo>/bar/. A /e will cause the replacement portion to be treated as a full-fledged Perl expression and evaluated right then and there. It is, however, syntax checked at compile-time. A second e modifier will cause the replacement portion to be evaled before being run as a Perl expression.

Examples:

s/\bgreen\b/mauve/g; # don't change wintergreen
$path =~ s|/usr/bin|/usr/local/bin|;
s/Login: $foo/Login: $bar/; # run-time pattern
($foo = $bar) =~ s/this/that/; # copy first, then change
$count = ($paragraph =~ s/Mister\b/Mr./g); # get change-count
$_ = 'abc123xyz';
s/\d+/$&*2/e; # yields 'abc246xyz'
s/\d+/sprintf(&quot;%5d&quot;,$&)/e; # yields 'abc 246xyz'
s/\w/$& x 2/eg; # yields 'aabbcc 224466xxyyzz'
s/%(.)/$percent{$1}/g; # change percent escapes; no /e
s/%(.)/$percent{$1} || $&/ge; # expr now, so /e
s/^=(\w+)/&pod($1)/ge; # use function call
# expand variables in $_, but dynamics only, using
# symbolic dereferencing
s/\$(\w+)/${$1}/g;
# Add one to the value of any numbers in the string
s/(\d+)/1 + $1/eg;
# This will expand any embedded scalar variable
# (including lexicals) in $_ : First $1 is interpolated
# to the variable name, and then evaluated
s/(\$\w+)/$1/eeg;
# Delete (most) C comments.
$program =~ s {
/\* # Match the opening delimiter.
.*? # Match a minimal number of characters.
\*/ # Match the closing delimiter.
} []gsx;
s/^\s*(.*?)\s*$/$1/; # trim white space in $_, expensively
for ($variable) { # trim white space in $variable, cheap
s/^\s+//;
s/\s+$//;
}
s/([^ ]*) *([^ ]*)/$2 $1/; # reverse 1st two fields
Note the use of $ instead of \ in the last example. Unlike sed, we use the \<digit> form in only the left hand side. Anywhere else it's $<digit>.

Occasionally, you can't use just a /g to get all the changes to occur that you might want. Here are two common cases:

# put commas in the right places in an integer
1 while s/(\d)(\d\d\d)(?!\d)/$1,$2/g;
# expand tabs to 8-column spacing
1 while s/\t+/' ' x (length($&)*8 - length($`)%8)/e;

The above explination is from the man pages




haunter@battlestrata.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top