For Programmers: Free Programming Magazines  


Home > Archive > PERL Programming > April 2004 > Forming a URL with Perl









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Forming a URL with Perl
Daniel Horn

2004-04-24, 10:31 am

Can someone tell me how to use some variables and some constants
to form a URL to be sent to another site to pass information? The
form of the URL takes:

http://www.domain.com/cgi-bin/prog....rt3:part4:part5

Why is it this below work in one program by sending the URL
as a redirect and in another as it just prints to the screen? And
how can I get it to redirect the above URL to the "domain.com"
site, not print it to the screen?


if ($url =~ "") {
print "Location:
[url]http://www.domain.com/cgi-bin/prog.pl? stuff=part1:part2:part3:part4:part5\n\n[
/url]";






Scott Bryce

2004-04-24, 11:30 am

Daniel Horn wrote:

> Why is it this below work in one program by sending the URL
> as a redirect and in another as it just prints to the screen? And
> how can I get it to redirect the above URL to the "domain.com"
> site, not print it to the screen?
>
>
> if ($url =~ "") {
> print "Location:
> [url]http://www.domain.com/cgi-bin/prog.pl? stuff=part1:part2:part3:part4:part5\n\n[
/url]";



Perhaps you have already sent a Content-Type header in the script that
doesn't work?

Daniel Horn

2004-04-24, 12:30 pm



Scott Bryce wrote:

> Daniel Horn wrote:
>
>
> Perhaps you have already sent a Content-Type header in the script that
> doesn't work?


Huh? ;-)

Although I know how to program in other languages, I am very
new to perl. I really don't need the if ($url =~ "") part, I just
need it to redirect the the above URL to the domain.com
and not print it to the screen. What can I do to force it to
(I don't know) clear or restart the "sent a Content-Type header
in the script" whatever that means? Like I say, I am new and
don't have a clue what that is. I am just copy and pasting stuff
in trying to get it to work.

Thanks



Daniel Horn

2004-04-24, 12:30 pm



Scott Bryce wrote:

> Daniel Horn wrote:
>
>
> Perhaps you have already sent a Content-Type header in the script that
> doesn't work?



OK, I looked at the program and this is what I think you are
talking about (maybe). Here is the code

print "Content-type: text/html\n\n";

if ($mystuff eq $stuff) {
if ($type eq "add") {

open (OUT, ">>$usedfile") || die "Can't open $usedfile\n";
$part1 = ($part2, "Md");
print OUT "$part3:$part1\n";
close OUT;
print "Location:
[url]http://www.domain.com/cgi-bin/prog.pl? stuff=part1:part2:part3:part4:part5\n\n[
/url]";
}
&writelogfile();
}


Right before the code

print "Location:
[url]http://www.domain.com/cgi-bin/prog.pl? stuff=part1:part2:part3:part4:part5\n\n[
/url]";

can I put something else other than:

print "Content-type: text/html\n\n";

Then right after it chang it back to

print "Content-type: text/html\n\n";

within the if statement?



Gunnar Hjalmarsson

2004-04-24, 1:30 pm

Daniel Horn wrote:
> Scott Bryce wrote:
>
> OK, I looked at the program and this is what I think you are
> talking about (maybe). Here is the code
>
> print "Content-type: text/html\n\n";


Yes, that statement prints a blank line, which is the reason why the
script prints to screen instead of redirecting.

<snip>

> Right before the code
>
> print "Location:
> [url]http://www.domain.com/cgi-bin/prog.pl? stuff=part1:part2:part3:part4:part5\n\n[
/url]";
>
> can I put something else other than:
>
> print "Content-type: text/html\n\n";
>
> Then right after it chang it back to
>
> print "Content-type: text/html\n\n";
>
> within the if statement?


No, that's not how it works. Once something has been printed, it can't
be undone.

If there are situations when you want the script to print to screen,
you need that line, but you'd better move it so that it's not executed
when the script is supposed to redirect.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Daniel Horn

2004-04-24, 1:30 pm



Gunnar Hjalmarsson wrote:

> Daniel Horn wrote:
>
> Yes, that statement prints a blank line, which is the reason why the
> script prints to screen instead of redirecting.


In a program that just updates files and does no printing to a screen,
why then would this be in there? This is a can program for updating files.


>
>
> <snip>
>
>
> No, that's not how it works. Once something has been printed, it can't
> be undone.


Even if the printing is to a file?

>
>
> If there are situations when you want the script to print to screen,
> you need that line, but you'd better move it so that it's not executed
> when the script is supposed to redirect.


Say I'm new to planet Perl, how could the program update the
files as is its design and as well as update a mirror site, which is
my desire? In other words I don't want to screw up the program,
but I don't want to constantly manually update the mirror site
either.






Scott Bryce

2004-04-24, 1:30 pm

Daniel Horn wrote:

> Although I know how to program in other languages, I am very
> new to perl.


Your question isn't really a Perl question. It is a CGI question. No
matter what language you are using to write CGI scripts, you would still
have the same problem.

Gunnar already answered your question, but the short answer is that you
need to restructure your code so that you determine whether to redirect
or send content to the browser before you send any headers.

> OK, I looked at the program and this is what I think you are
> talking about (maybe). Here is the code



Yup!

Perhaps you want something more like:

> if ($mystuff eq $stuff) {
> if ($type eq "add") {
> open (OUT, ">>$usedfile") || die "Can't open $usedfile\n";
> $part1 = ($part2, "Md");
> print OUT "$part3:$part1\n";
> close OUT;
> print "Location:
> [url]http://www.domain.com/cgi-bin/prog.pl? stuff=part1:part2:part3:part4:part5\n\n[
/url]";
> }

else {
print "Content-type: text/html\n\n";
# The rest of the output goes here.
}

> &writelogfile(); # Don't use & to call subroutines
> }



> I am just copy and pasting stuff in trying to get it to work.


What you are copying could be better written.

> if ($mystuff eq $stuff) {


Not very descriptive variable names.


> if ($type eq "add") {


if ($type eq 'add'){

Double quotes are not necessary.

> open (OUT, ">>$usedfile") || die "Can't open $usedfile\n";


open (OUT, ">>$usedfile") || die "Can't open $usedfile: $!";

Let Perl tell you why the open failed.

> $part1 = ($part2, "Md");


$part1 = ($part2, 'Md');

Though I doubt this is returning what you expect it to.


> print OUT "$part3:$part1\n";
> close OUT;
> print "Location:
>

[url]http://www.domain.com/cgi-bin/prog.pl? stuff=part1:part2:part3:part4:part5\n\n[
/url]";
> }
> &writelogfile();


writelogfile();

Don't use & to call subroutines

> }




Gunnar Hjalmarsson

2004-04-24, 2:33 pm

Daniel Horn wrote:
> Gunnar Hjalmarsson wrote:
>
> In a program that just updates files and does no printing to a
> screen, why then would this be in there?


If that's the case, you can simply remove it.

> Say I'm new to planet Perl,


As Scott explained, the problem has nothing to do with the programming
language, which in this case happens to be Perl. Your questions are
about CGI and HTTP.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Daniel Horn

2004-04-24, 2:33 pm



Scott Bryce wrote:

> Daniel Horn wrote:
>
>
> Your question isn't really a Perl question. It is a CGI question. No
> matter what language you are using to write CGI scripts, you would still
> have the same problem.
>
> Gunnar already answered your question, but the short answer is that you
> need to restructure your code so that you determine whether to redirect
> or send content to the browser before you send any headers.
>
>
> Yup!
>
> Perhaps you want something more like:
>
> else {
> print "Content-type: text/html\n\n";
> # The rest of the output goes here.
> }




Thanks for the help, but will the original canned program still
print the information it was originally intended to the files? I
want to add the update to a mirror site for myself, but I do not
want to screw up the original program. And the above (I mangled
together) is an example, and the original has a "else" in it, could
I add your example to the end before it exits the if statement
and it would do whatever it needs to? I really don't know why it
is there in the first place, as far as I know this program does
nothing but write to a file.

thanks
[color=darkred]
>
>
>
>
> What you are copying could be better written.
>
>
> Not very descriptive variable names.
>
>
> if ($type eq 'add'){
>
> Double quotes are not necessary.
>
>
> open (OUT, ">>$usedfile") || die "Can't open $usedfile: $!";
>
> Let Perl tell you why the open failed.
>
>
> $part1 = ($part2, 'Md');
>
> Though I doubt this is returning what you expect it to.
>
> [url]http://www.domain.com/cgi-bin/prog.pl? stuff=part1:part2:part3:part4:part5\n\n[
/url]";
>
> writelogfile();
>
> Don't use & to call subroutines
>



Scott Bryce

2004-04-24, 2:33 pm

Daniel Horn wrote:

> In a program that just updates files and does no printing to a screen,
> why then would this be in there? This is a can program for updating files.


Again, we are dealing with a CGI issue, not a Perl issue.

A CGI program, not matter what language it is written in MUST return
SOMETHING to the browser. That is probably why the Content-type header
is being sent to the browser.

I think a question about sending a "please do nothing" header to the
browser has been answered at comp.perl.language.misc recently. You might
want to take a look there. But I would think that at the very least, you
would want to send some sort of Success/Failure message to the browser.

> Say I'm new to planet Perl, how could the program update the
> files as is its design and as well as update a mirror site, which is
> my desire?


1) Define the problem more clearly. What exactly do you want to accomplish?

2) Ask in a cgi newsgroup such as comp.infosystems.www.authoring.cgi

When you have the CGI issues worked out, give your Perl script your best
shot, then ask your Perl questions in comp.perl.language.misc. But,
please, read the posting guidelines in those two newsgroups first.

You will have better success getting your questions answered if you can
show that you have done your homework, so make some attempt at finding
the answers on your own. You will also understand the answers better
after you have done some digging on your own.

Questions like "I found this old buggy script on the internet, now help
me modify it." don't fly well at comp.perl.language.misc.

Scott Bryce

2004-04-24, 3:30 pm

Daniel Horn wrote:

> Thanks for the help, but will the original canned program still
> print the information it was originally intended to the files?


I would have no way of knowing without seeing more code.

> I
> want to add the update to a mirror site for myself, but I do not
> want to screw up the original program. And the above (I mangled
> together) is an example, and the original has a "else" in it, could
> I add your example to the end before it exits the if statement
> and it would do whatever it needs to?


It is probably not that simple.

It appears that you don't have enough of a programming background to
tackle this problem on your own. Newsgroups such as this one are not a
good place to ask for people to spend large amounts of time helping you
modify canned scripts.

> I really don't know why it
> is there in the first place, as far as I know this program does
> nothing but write to a file.


It (The Content-type header, I assume) is there because a CGI script
MUST return SOMETHING to the browser.

As Gunnar pointed out, your question is a CGI/HTTP question and not a
Perl question.

Daniel Horn

2004-04-24, 4:30 pm



Gunnar Hjalmarsson wrote:

> Daniel Horn wrote:
>
> If that's the case, you can simply remove it.


Maybe as Scott pointed it (it seems) whoever wrote the canned
program was not that good at it and just stuck it in there thinking
it was needed, but then Scott also said you have to send something
to the browser, but in a automated system I would not think
there is a browser, then again I don't know. I'm ignorant at this
point.

>
>
>
> As Scott explained, the problem has nothing to do with the programming
> language, which in this case happens to be Perl. Your questions are
> about CGI and HTTP.


Well at this point as ignorant as I am, I'm not sure how
I would have known that. But between you two I thank
you and think I at least know what to ask and look for.

Thanks.



Scott Bryce

2004-04-24, 6:33 pm

Several sections of this thread brought together here rather than
spreading this post across several sub-threads.

Daniel Horn wrote:

> Maybe as Scott pointed it (it seems) whoever wrote the canned
> program was not that good at it and just stuck it in there thinking
> it was needed, but then Scott also said you have to send something
> to the browser, but in a automated system I would not think
> there is a browser, then again I don't know. I'm ignorant at this
> point.


This is why it is important to define the problem. I don't understand
why you are sending http headers, if this is not a CGI script
interacting with a browser. In what other environment are you sending
http headers? Is there a user agent of some sort involved in what you
are doing? What are you trying to redirect, if not a browser?

>
>
> Nothing but sending that URL, it does all the rest after that.
> I just need to send that URL that's it, that is the definition
> and what I want to accomplish.


Then you probably don't want to send a Content-type header at all either
in an else statement or otherwise. The code snippet you posted only
sends the Location header if certain conditions are true. What is
supposed to happen if those conditions are not true?

If there is no browser involved, what is the redirect header supposed to
accomplish?

> Well that is easier to say when you know the questions to
> ask and what to look for, as I assumed it was a perl question,
> and you both say it is not, you suggested it was the
> Content-Type header which I had no idea what that was so
> could not have known to ask about it.


Understood. This is a common problem when working in an unfamiliar
environment.

> So in knowing a
> subject you invariable assume they know the simple things
> you do not remember you learned in the beginning. In
> other words you can't ask the right question nor look for
> the right answer if you don't even know what to look for.


In that circumstance, it may be better to learn the subject better
before posting ambiguous questions about code you can't read.

> Not in perl, I know the old languages as dbase, clipper,
> basic, and cobal. This is a new language.


You asked a question about where a statement should go in within an
if/else structure. That isn't the kind of question I would expect to see
from someone with your background.

> And I don't
> need to know how to program at this moment, I just need
> to be able to send that URL,


We need to know what "send that URL" means.

> the canned program does the
> rest, I just want to send that URL to the mirror site as well,


Again, we need to know what "send that URL" means. Your OP mentioned
redirects, but now you say there is no browser involved. But the code:

print "Location:
[url]http://www.domain.com/cgi-bin/prog.pl? stuff=part1:part2:part3:part4:part5\n\n[
/url]";


is intended to redirect a browser to a new page.

If that is not what you want to do, then what does it mean to "send that
URL to the mirror site?" What is the mirror site going to do with the
URL once it is sent there?

> why so hostile about this, I'm trying to be nice and
> only want help.


I'm not being hostile. I'm only suggesting a better place to look for
the help you appear to need. But now you say there is no browser
involved, so I'm not sure what help you need. It could be that you are
in an unfamiliar environment and part of the help you need is defining
the problem within that environment.

To help us help you better:

1) Tell us exactly what you want to accomplish.

2) Post the code you have tried. If the code is very long, extract the
part thay you need help with and create a concise, but complete script
that demonstrates your problem.

3) Tell us what you expect the script to do and what it is doing that is
different than you expected.

Daniel Horn

2004-04-24, 7:30 pm



Scott Bryce wrote:

> Several sections of this thread brought together here rather than
> spreading this post across several sub-threads.
>
> Daniel Horn wrote:
>
>
> This is why it is important to define the problem. I don't understand
> why you are sending http headers, if this is not a CGI script
> interacting with a browser. In what other environment are you sending
> http headers? Is there a user agent of some sort involved in what you
> are doing? What are you trying to redirect, if not a browser?


Well what I did was just take it out of the if statement and
put it above the

print "Content-type: text/html\n\n";

and it works fine, but it prints a extra blank line in file which
does not seem to hurt any function, so I can live with it.
I only had until Wednesday to get this working so I had
no time for theory and research. I thank you very much
for your help.

Thanks again.

Hot damn I got it done before the deadline.

>
>
>
> Then you probably don't want to send a Content-type header at all either
> in an else statement or otherwise. The code snippet you posted only
> sends the Location header if certain conditions are true. What is
> supposed to happen if those conditions are not true?
>
> If there is no browser involved, what is the redirect header supposed to
> accomplish?
>
>
> Understood. This is a common problem when working in an unfamiliar
> environment.
>
>
> In that circumstance, it may be better to learn the subject better
> before posting ambiguous questions about code you can't read.
>
>
> You asked a question about where a statement should go in within an
> if/else structure. That isn't the kind of question I would expect to see
> from someone with your background.
>
>
> We need to know what "send that URL" means.
>
>
> Again, we need to know what "send that URL" means. Your OP mentioned
> redirects, but now you say there is no browser involved. But the code:
>
> print "Location:
> [url]http://www.domain.com/cgi-bin/prog.pl? stuff=part1:part2:part3:part4:part5\n\n[
/url]";
>
> is intended to redirect a browser to a new page.
>
> If that is not what you want to do, then what does it mean to "send that
> URL to the mirror site?" What is the mirror site going to do with the
> URL once it is sent there?
>
>
> I'm not being hostile. I'm only suggesting a better place to look for
> the help you appear to need. But now you say there is no browser
> involved, so I'm not sure what help you need. It could be that you are
> in an unfamiliar environment and part of the help you need is defining
> the problem within that environment.
>
> To help us help you better:
>
> 1) Tell us exactly what you want to accomplish.
>
> 2) Post the code you have tried. If the code is very long, extract the
> part thay you need help with and create a concise, but complete script
> that demonstrates your problem.
>
> 3) Tell us what you expect the script to do and what it is doing that is
> different than you expected.




Scott Bryce

2004-04-27, 12:44 am

Daniel Horn wrote:

> Well what I did was just take it out of the if statement and
> put it above the
>
> print "Content-type: text/html\n\n";
>
> and it works fine, but it prints a extra blank line in file which
> does not seem to hurt any function, so I can live with it.


A browser redirect does not print anything to a file, so I doubt that
that is what is giving you the extra blank line.

> I thank you very much
> for your help.


You're welcome, I guess, but at this point I'm not sure whether anything
I wrote had anything to do with your original problem.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com