Gossamer Forum
Home : General : Perl Programming :

Possible to do print $outputType; where $outputType="<<EOF"

Quote Reply
Possible to do print $outputType; where $outputType="<<EOF"
I'm very new to Perl/CGI unfortuantely and have done C++ for some years so am probably trying to mix the two badly :)

I am trying to achieve the following :

if($submit eq "Preview")
{
$outputType="<<EOF";

output($outputType);

}

sub output {
($outputType, $outputTypeShort) = @_;

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

print $outputType;
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Special Offer</title>
<link rel="stylesheet" type="text/css" href="specials.css">
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

EOF

}

I'm not sure if I'm passing the variable correctly etc, but what I am trying to achieve is to call a 'Function' Ouput which either prints to screen, prints to file, or does both. I was aiming to do this with 1 output function which took a parameter which either stored "<<EOF" or "OUT<<EOF" so that when I called print $outputType; it would print to the correct place. I presume I'm either doing something wrong or what I'm trying to do is completely impossible. I get the impression that its ok to call the variable to be printed out, e.g. print "$outputType"; but its not possible to do print $outputType; ....OUPUT_HERE.... EOF

I appreciate any help,

Thanks,
Stuart.
me@stu.uk.com
Quote Reply
Re: [-=Stu=-] Possible to do print $outputType; where $outputType="<<EOF" In reply to
Yup...your pretty confused with it Tongue

Code:
:

if($submit eq "Preview")
{
$outputType="<<EOF";

&output($outputType);

}

sub output {
($outputType, $outputTypeShort) = @_;
Not sure about this part, I would use shift..where are you getting $outputTypeShort from?

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

print $outputType;
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Special Offer</title>
<link rel="stylesheet" type="text/css" href="specials.css">
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

EOF

}

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy.] Possible to do print $outputType; where $outputType="<<EOF" In reply to
Mommmyy :)

I don't know what to do. I really need to get this working but am completely stuck. I thought if I called the 'funciton' using
output($outputType);

and then used

($outputType) = @_;


inside the function, this would take the passed variable and put in into $outputType inside the 'function'. Obviously not Mad, how do I pass $outputType into the function and call use it/reference it using $variable inside the funciton.

The main question I really have is whether doing print $outputType; when $outputType="<<EOF" is the same as doing print <<EOF; and should/would this work ?

Ignore $outputTypeShort for the minute, thats another variable which I may want to pass, but I could survive with only passing the one.

Thanks for your help,
Stuart.

In Reply To:
Yup...your pretty confused with it Tongue

Code:
:

if($submit eq "Preview")
{
$outputType="<<EOF";

&output($outputType);

}

sub output {
($outputType, $outputTypeShort) = @_;
Not sure about this part, I would use shift..where are you getting $outputTypeShort from?


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

print $outputType;
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Special Offer</title>
<link rel="stylesheet" type="text/css" href="specials.css">
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

EOF

}
Quote Reply
Re: [Andy.] Possible to do print $outputType; where $outputType="<<EOF" In reply to
>>
Yup...your pretty confused with it
<<

I guess you are too :)


&output($outputType);

.....the & isn't needed.
Quote Reply
Re: [-=Stu=-] Possible to do print $outputType; where $outputType="<<EOF" In reply to
Im not totally clear on what you want to do but is it something like ?:

Code:

output($outputType) if ($submit eq 'Preview');

sub output {
#--------------------------------------

my $type = shift;

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

?

....if you are wanting to write to a file then you need to open/close one :)

Last edited by:

Paul: May 8, 2002, 4:42 AM
Quote Reply
Re: [Paul] Possible to do print $outputType; where $outputType="<<EOF" In reply to
What I'm trying to do, ignoring the subroutine thing for the minute is set a variable $outputType to either "<<EOF" or "OUT<<EOF" so I can use them in the following way :

If I want to print to screen
$outputType="<<EOF"

print $outputType;
<html>
<body>
.......

EOF

if I want ot print to file
$outputType="OUT<<EOF"

open(OUT,">output.html");

print $outputType;
<html>
<body>
.......

EOF

Therefore if its possible to do the above with print $outputType; so that it alters where it outputs to, I can create the 'function' that accepts that as a parameter so I only have to write 1 output routine and this output routine writes to screen or file depending on what $outputType is set to when its passed to the routine.

What I suspected at the minute (ignoring the few typo's that I made above) is that it isn't possible to do print $outputType; so that CGI/Perl interprets that as being print <<EOF; or print OUT<<EOF; ?

Thanks for any help,
Stuart.
Quote Reply
Re: [Paul] Possible to do print $outputType; where $outputType="<<EOF" In reply to
In Reply To:
>>
Yup...your pretty confused with it
<<

I guess you are too :)


&output($outputType);

.....the & isn't needed.

But doesn't hurt?

- wil
Quote Reply
Re: [Wil] Possible to do print $outputType; where $outputType="<<EOF" In reply to
As mentioned a while back:

http://www.perldoc.com/...6.1/pod/perlsub.html

In this case it is optional.....so it wasn't something that needed correcting...
Quote Reply
Re: [Paul] Possible to do print $outputType; where $outputType="<<EOF" In reply to
Yes...but its something that SHOULD be corrected...otherwise you slip into bad habits Wink

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy.] Possible to do print $outputType; where $outputType="<<EOF" In reply to
>>Yes...but its something that SHOULD be corrected...otherwise you slip into bad habits <<

Huh?....its not a bad habit...read the perldoc page.
Quote Reply
Re: [Andy.] Possible to do print $outputType; where $outputType="<<EOF" In reply to
What bad habbit? Unsure

Can you see me smoking behind my desk? ;-)

- wil
Quote Reply
Re: [Wil] Possible to do print $outputType; where $outputType="<<EOF" In reply to
Argh..there is no point trying to fight an unwinnable battle with Paul...he is obviously right Wink All I am saying is that its a good idea to get into using &'s in front of subroutines, as that is what every good Perl book, and tutorials on the Internet says to do! Not that I would know what I'm talking about of course Tongue

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy.] Possible to do print $outputType; where $outputType="<<EOF" In reply to
Chill, Winston! :-)

- wil
Quote Reply
Re: [Wil] Possible to do print $outputType; where $outputType="<<EOF" In reply to
Erm Unsure

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Post deleted by Wil In reply to

Last edited by:

Wil: May 8, 2002, 6:07 AM
Quote Reply
Re: [Wil] Possible to do print $outputType; where $outputType="<<EOF" In reply to
Stop arguing and help fix my problem Smile

Ok I've sorted of fixed it myself only need one more bit of advice. I have now decided to use ' select OUT; ' to output for files and then just continue using norm print <<EOF; ........ EOF.

My final question is how do alter select to change back from using ' select OUT; ' to outputing back to the default which is the screen ? I tried just doing select; but this just stop the script from running. I presume there is a specific select option to output to the screen ?

Thanks again,
Stuart.
Quote Reply
Re: [-=Stu=-] Possible to do print $outputType; where $outputType="<<EOF" In reply to
Im guessing it would be select(STDOUT);
Quote Reply
Re: [Paul] Possible to do print $outputType; where $outputType="<<EOF" In reply to
Last question, promise :) Everything is finall finished hehe. My last thing is probably extremly trivial. I have :

if($submitButton eq "Authorise Offer")
{

}

I want this to rename a file, and then make the browser to open the file. I think I have the rename bit sorted using :

rename $previewFile,$submitFile;

But how do I then say Open $submitFile etc ?

Thanks for all your help,
Stuart.
Quote Reply
Re: [-=Stu=-] Possible to do print $outputType; where $outputType="<<EOF" In reply to
http://www.perldoc.com/...1/pod/func/open.html
Quote Reply
Re: [Paul] Possible to do print $outputType; where $outputType="<<EOF" In reply to
Hi Paul

By the way... I was reading Programming Perl, 3rd edition last night and there's some info about this in it. It states when sub routines should be prepended with an & and where it is OK to do without.

I'll have to find the page number for you.. do you have that book?

I can't really remember much of what was discussed, but I remember it mentioning something about how @_ is passed to the sub routine in question, and how by calling a sub prepended with & made a difference to the scope of @_.

As I said, I'll try and find the page ref. for you tonight.

- wil
Quote Reply
Re: [Wil] Possible to do print $outputType; where $outputType="<<EOF" In reply to
Yep thats correct &sub() circumvents prototypes (ie it avoids them)

&sub makes @_ visible to the called subroutine.

But the & is optional if you use ()


NAME(LIST); # & is optional with parentheses.
NAME LIST; # Parentheses optional if predeclared/imported.
&NAME(LIST); # Circumvent prototypes.
&NAME; # Makes current @_ visible to called subroutine

Last edited by:

Paul: May 10, 2002, 1:54 AM
Quote Reply
Re: [Paul] Possible to do print $outputType; where $outputType="<<EOF" In reply to
Yep, it was something like that. The parenthesis is actually the important part.

- wil