Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > File handle Variable scope issue

Reply
Thread Tools

File handle Variable scope issue

 
 
Truthless
Guest
Posts: n/a
 
      12-30-2003
Hello All,

I am somewhat new to perl and I am having some difficulty with a cgi
script that I am making. This script is supposed to read a html file and
replace certain sections of it with its own variables.

Code similar to this works great. as long as $html is in the actual script.

$variables = "Hello World";
$html = '<h1>'.$variables.'</h1>';

sub print_html {
print "Content-type:text/html\n\n";
print <<End_of_Bottom;
$html
End_of_Bottom
}

This outputs <h1>Hello World</h1>.

The issue I am having is getting the $variables to be parsed if the html
is pulled from a file. As I am doing here...

$confile = 'myfile.html'; #file with <h1>$variables</h1> in it

sub openconf {
open(content,"$confile") or dienice("Cant open $confile : $!");
@ary = <content>;
close(content);
foreach $line (@ary) {
chomp($line);
print "$line\n";
}
}

This will print <h1>$variables</h1>, rather than the desired <h1>Hello
World</h1>.

Could any of you please tell me what I can do to make the variables in
the strings that come from the file handle actually evaluate? There must
be something basic I am looking for.

Thank you for any help.


T.

 
Reply With Quote
 
 
 
 
Ragnar Hafstaš
Guest
Posts: n/a
 
      12-31-2003
"Truthless" <> wrote in message
news:sIoIb.189489$...

[snipped why are not variables like here-documents]

> Could any of you please tell me what I can do to make the variables in
> the strings that come from the file handle actually evaluate? There must
> be something basic I am looking for.


yes to make the variables actually *evaluate*, you could use *eval*

this is discussed in fact in the FAQ:
perldoc -q "How can I expand variables in text strings"

personally I prefer to use a hash:

$subs=(a=>'b', foo=>'bar');
$text='some text containing easily matched markers like %a% or %foo%';
$text=~s/%(\w+)%/$subs{$1}/g;

this way, $text can safely be read from a file that others can modify, even
if i dont trust them
not to put something nasty in it.

gnari




 
Reply With Quote
 
 
 
 
Ragnar Hafstaš
Guest
Posts: n/a
 
      12-31-2003
"Truthless" <> wrote in message
news:sIoIb.189489$...

[snipped variable expantion puzzle]

wierd choice of Subject line, by the way.

gnari




 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      12-31-2003
Truthless <> wrote:

> This script is supposed to read a html file and
> replace certain sections of it with its own variables.



There are plenty of already designed, implemented, tested and
supported templating systems.

I'd use one of those rather than invent yet another one...

You don't need to do this. It has already been done (repeatedly).


> The issue I am having is getting the $variables to be parsed if the html
> is pulled from a file. As I am doing here...



> open(content,"$confile") or dienice("Cant open $confile : $!");



That will break when the Next Version of Perl introduces a
new function named content().

You should use UPPER CASE for filehandles.

You also have a useless use of double quotes, see the Perl FAQ:

What's wrong with always quoting "$vars"?


open(CONTENT, $confile) or dienice("Cant open '$confile' : $!");


> @ary = <content>;


my $html = join '', <CONTENT>; # a slow and tedious way of slurping


> There must
> be something basic I am looking for.



Lookup the $/ variable in perlvar.pod.

Enable slurp mode.


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Iain Chalmers
Guest
Posts: n/a
 
      12-31-2003
In article <>,
(Tad McClellan) wrote:

> Truthless <> wrote:
>
> > @ary = <content>;

>
> my $html = join '', <CONTENT>; # a slow and tedious way of slurping


<snip>
>
>
> Lookup the $/ variable in perlvar.pod.
>
> Enable slurp mode.


You might also want to read:

http://www.perl.com/pub/a/2003/11/21/slurp.html

"Perl Slurp-Eaze
by Uri Guttman"


cheers,

big

--
'When I first met Katho, she had a meat cleaver in one hand and
half a sheep in the other. "Come in", she says, "Hammo's not here.
I hope you like meat.' Sharkey in aus.moto
 
Reply With Quote
 
Truthless
Guest
Posts: n/a
 
      12-31-2003
Iain Chalmers wrote:
> In article <>,
> (Tad McClellan) wrote:
> <snip>
>>Lookup the $/ variable in perlvar.pod.
>>
>>Enable slurp mode.

>
> You might also want to read:
>
> http://www.perl.com/pub/a/2003/11/21/slurp.html
> big
>



Thanks, but I still have questions.
I read that link and what I could take from it I've tried to use.

$var = "Hello World";
sub openconf {
local( $/, *CONF ) ;
open(CONF,$confile) or dienice("Cant open $confile : $!");
$text = <CONF>
}
print "$text";

The trouble is that
print "$text"; gives me the literal strings from the file and goes not
expand the variables within. I still get <h1>$var</h1>. I am not certain
what is needed to make it display <h1>Hello World</h1> The perldocs are
very good but I think I need an example. I see a lot of the docs have
regex or something like this $text =~ s/(\$\w+)/$1/eeg; I am not certain
what direction I should be going here. If I plant $vars in any
other type of string variable it works great.

$var = "Hello World";
$text = "This is a string of text $var";
print "$text";


The above outputs


This is a string of text Hello World.

Why is it that when the string of text comes from a file handle it does
not expand? What can I do to make it expand properly?

Thank you.

 
Reply With Quote
 
Truthless
Guest
Posts: n/a
 
      12-31-2003
Tad McClellan wrote:
> Truthless <> wrote:


> There are plenty of already designed, implemented, tested and
> supported templating systems.
>
> I'd use one of those rather than invent yet another one...
>
> You don't need to do this. It has already been done (repeatedly).


That won't help me learn CGI

> Lookup the $/ variable in perlvar.pod.
>
> Enable slurp mode.


I think I have the slurp down but the variables do not expand.

sub openconf {
local( $/, *CONF ) ;
open(CONF,$confile) or dienice("Cant open $confile : $!");
$text = <CONF>
}


Any ideas. The perldocs are very complete but a little much I just need
an example.

Thanks in advance.

T

 
Reply With Quote
 
Truthless
Guest
Posts: n/a
 
      12-31-2003
Gunnar Hjalmarsson wrote:
> Truthless wrote:
>
>> The trouble is that print "$text"; gives me the literal strings


>> lot of the docs have regex or something like this
>> $text =~ s/(\$\w+)/$1/eeg;


> But that is an example! Why don't you try it in your code?


> $text = 'This is a string of text $var';
> ------------^-----------------------------^
>
> you notice a difference. You need to understand the difference between
> a literal and an interpolated string, i.e. the difference between
> using single-quotes and double-quotes when quoting a string.
>
>

Hello,

sub openconf {
local( $/, *CONF ) ;
open(CONF,$confile) or dienice("Cant open $confile : $!");
$text = <CONF>;
$text =~ s/(\$\w+)/$1/eeg;
}


The $text =~ s/(\$\w+)/$1/eeg; trick worked but I am not certain what it
is doing exactly. Is it regex that simply looks for $anything ? I guess
I need to learn more regex.
Double quotes and single quotes as with many scripting languages are the
same. Usually " means interpret and ' means literally. I had more than
one response mention the quotes, I was not sure exactly what the quoting
issue was since I WAS using " and I wanted variables expanded.

Thanks for the sugestion,
perhaps some one could parphrase the
$text =~ s/(\$\w+)/$1/eeg; assignment for me.

TIA,

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      12-31-2003
Truthless wrote:
> The trouble is that print "$text"; gives me the literal strings
> from the file and goes not expand the variables within. I still get
> <h1>$var</h1>.


<snip>

> The perldocs are very good but I think I need an example. I see a
> lot of the docs have regex or something like this
> $text =~ s/(\$\w+)/$1/eeg;


But that is an example! Why don't you try it in your code?

> If I plant $vars in any other type of string variable it works
> great.
>
> $var = "Hello World";
> $text = "This is a string of text $var";
> print "$text";


But if you do:

$text = 'This is a string of text $var';
------------^-----------------------------^

you notice a difference. You need to understand the difference between
a literal and an interpolated string, i.e. the difference between
using single-quotes and double-quotes when quoting a string.

http://www.perldoc.com/perl5.8.0/pod...yntax-overview

When you read data from a file, nothing gets interpolated, so it is
similar to using single-quotes, so to say.

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

 
Reply With Quote
 
Gregory Toomey
Guest
Posts: n/a
 
      12-31-2003
It was a dark and stormy night, and Truthless managed to scribble:

> Hello All,
>
> I am somewhat new to perl and I am having some difficulty with a cgi
> script that I am making. This script is supposed to read a html file and
> replace certain sections of it with its own variables.
>
> Code similar to this works great. as long as $html is in the actual
> script.
>
> $variables = "Hello World";
> $html = '<h1>'.$variables.'</h1>';
>
> sub print_html {
> print "Content-type:text/html\n\n";
> print <<End_of_Bottom;
> $html
> End_of_Bottom
> }
>
> This outputs <h1>Hello World</h1>.
>
> The issue I am having is getting the $variables to be parsed if the html
> is pulled from a file. As I am doing here...
>
> $confile = 'myfile.html'; #file with <h1>$variables</h1> in it
>
> sub openconf {
> open(content,"$confile") or dienice("Cant open $confile : $!");
> @ary = <content>;
> close(content);
> foreach $line (@ary) {
> chomp($line);
> print "$line\n";
> }
> }
>
> This will print <h1>$variables</h1>, rather than the desired <h1>Hello
> World</h1>.
>
> Could any of you please tell me what I can do to make the variables in
> the strings that come from the file handle actually evaluate? There must
> be something basic I am looking for.
>
> Thank you for any help.
>


Err, yes. Your brain was at there I was at about 3 years ago. You can use Perl templates but there's nothing like getting your hands dirty!

Here's the Rolls Royce version of what you want:



open(INFILE, 'myfile.htm');
foreach (<INFILE>) {
s/(\$\w+(::\w+)?)/$1/eeg;
print;
}

WARNING: because this does command substitution and evaluation, it can there are security issues and is dangerous in the wrong hands.

This works with lexical variables like $var. It also works with package variables like $var1::var2.


Another question you will soon ask youself is "well if I can evaluate a variable, can I evaluate a function/subroutine." The answer is yes, providing you provide a return value & use the weird Perl syntax @{[subroutine_name()]}

EXAMPLE: define a subroutine testsub in your main program

sub testsub {return "This could be anything" }

Then myfile.html could contain the following 3 lines:
Hello World
@{[testsub()]}
Line 3


gtoomey


gtoomey
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CSPEC issue: lossing scope (or incorrect scope) in cspec subroutine. balldarrens@gmail.com Perl Misc 0 02-05-2009 08:42 PM
Having trouble understanding function scope and variable scope Andrew Falanga Javascript 2 11-22-2008 09:23 PM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM
File Handle Reading Blues: Rereading a File Handle for Input Dietrich Perl 1 07-22-2004 10:02 AM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57