Reading a file

Reading a file

am 19.09.2005 22:57:04 von Procor

Hello,

i'm new with perl.

I want to read a file and display this when visiting my website :

In my browser : http://www.website.nl/displayfile.pl
===================
#!/usr/local/bin/perl
$logurl = "/opt/guide/www.website.nl/log.txt";
# Begin the Editing of log File
print $logurl;
=====================
It doesn't work!!

Why?

Marco

Re: Reading a file

am 19.09.2005 23:30:35 von Jim Gibson

In article <432f261b$0$14382$ba620dc5@text.nova.planet.nl>, Procor
wrote:

> Hello,
>
> i'm new with perl.
>
> I want to read a file and display this when visiting my website :
>
> In my browser : http://www.website.nl/displayfile.pl
> ===================
> #!/usr/local/bin/perl
> $logurl = "/opt/guide/www.website.nl/log.txt";
> # Begin the Editing of log File
> print $logurl;
> =====================
> It doesn't work!!
>
> Why?

How does it not work?

You have to generate a complete HTTP response message, including the
appropriate headers. The easiest way to do that in Perl is to use the
CGI module and its header, start_html, and end_html subroutines.
Generating proper HTML for HTTP can be a complicated task, so you have
some studying to do. Start with 'perldoc CGI' for examples.

If you want to display the contents of a file, rather than just its
path name, you will have to open the file, read it, and print its lines
to the output. Look at Perl's open, print, and close functions, and the
'perldoc perlio' section of your online documentation.

FYI: the comp.lang.perl newsgroup is defunct. Try comp.lang.perl.misc
in the future or just stick with alt.perl.

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Re: Reading a file

am 20.09.2005 02:03:44 von jurgenex

Procor wrote:
> Hello,
>
> i'm new with perl.
>
> I want to read a file and display this when visiting my website :
>
> In my browser : http://www.website.nl/displayfile.pl
> ===================
> #!/usr/local/bin/perl
> $logurl = "/opt/guide/www.website.nl/log.txt";
> # Begin the Editing of log File
> print $logurl;
> =====================
> It doesn't work!!

Hmmm, strange. By all means your program should print the verbatim text

/opt/guide/www.website.nl/log.txt

to STDOUT, usually your terminal.
And it does when I run your snippet on my computer.

C:\tmp>t.pl
/opt/guide/www.website.nl/log.txt
C:\tmp>

What do you expect the code snippet to do instead?

> Why?

Unless you tell us what you expect this code snippet to do we cannot tell
what you did wrong because we don't know your goal.

jue

Re: Reading a file

am 20.09.2005 14:15:13 von Paul Lalli

Procor wrote:

[removed defunct newsgroup comp.lang.perl - please try
comp.lang.perl.misc next time around]

> I want to read a file and display this when visiting my website :
>
> In my browser : http://www.website.nl/displayfile.pl
> ===================
> #!/usr/local/bin/perl
> $logurl = "/opt/guide/www.website.nl/log.txt";
> # Begin the Editing of log File
> print $logurl;
> =====================
> It doesn't work!!
>
> Why?

What made you believe this piece of code would do anything other than
print the string you assigned to $logurl? Did you get this belief from
some site or book? If so, please let us know which one so we know to
avoid it like the plague.

You assigned a string to a variable $logurl. You then printed the
contents of that variable. The contents of that variable is the
string. The fact that that string happens to represent a file name is
wholly irrelevant.

To open a file in Perl, use the open() function. To read from a file,
use the <> operator or the readline() function.

You stated you are new to Perl. As that is the case, you would be well
served to start reading some documentation and tutorials:
perldoc -f open
perldoc -f readline
perldoc perlopentut

Paul Lalli

Re: Reading a file

am 28.09.2005 19:13:18 von bill

"Procor" wrote in message
news:432f261b$0$14382$ba620dc5@text.nova.planet.nl...
> Hello,
>
> i'm new with perl.
>
> I want to read a file and display this when visiting my website :
>
> In my browser : http://www.website.nl/displayfile.pl
> ===================
> #!/usr/local/bin/perl
> $logurl = "/opt/guide/www.website.nl/log.txt";
> # Begin the Editing of log File
> print $logurl;
> =====================
> It doesn't work!!
>
> Why?
>
> Marco
>
>
>
>

# added -w (warn) I think you will need it
#!/usr/local/bin/perl -w

# ok you defined what file you want to view
$logurl = "/opt/guide/www.website.nl/log.txt";

# this actually opens the file for use and returns any errors
open (FILE, "$logurl") or die "Couldn't open $logurl: $!";.

# you will need to call each line of the file for use.
# $_ is what you will be working with.
# With each pass through the loop $_ will have the new line data.
while () {
print $_; # what you do with each line in here is up to you.
}

close (FILE); # be nice and close the file

Hope that helps,
Bill

Re: Reading a file

am 29.09.2005 14:21:55 von Paul Lalli

[trimmed newsgroups - there's no such group as comp.lang.perl]

Bill wrote:
> # added -w (warn) I think you will need it
> #!/usr/local/bin/perl -w

That -w is rendered useless when you put a comment above the shebang.
The shebang must always be the absolute first sequence of characters in
the file, prior to any code, comments, or whitespace.

Paul Lalli

Re: Reading a file

am 29.09.2005 18:31:46 von bill

"Paul Lalli" wrote in message
news:1127996515.401840.89550@z14g2000cwz.googlegroups.com...
> [trimmed newsgroups - there's no such group as comp.lang.perl]
>
> Bill wrote:
> > # added -w (warn) I think you will need it
> > #!/usr/local/bin/perl -w
>
> That -w is rendered useless when you put a comment above the shebang.
> The shebang must always be the absolute first sequence of characters in
> the file, prior to any code, comments, or whitespace.
>
> Paul Lalli
>


100% right Paul.

I should have commented after in case Marco directly cut and pasted from the
text of the message.

Sorry for the bad.

Bill

Re: Reading a file

am 03.10.2005 19:34:47 von jj

I have run into that before and it never worked for me

Bill wrote:

>
> "Paul Lalli" wrote in message
> news:1127996515.401840.89550@z14g2000cwz.googlegroups.com...
>> [trimmed newsgroups - there's no such group as comp.lang.perl]
>>
>> Bill wrote:
>> > # added -w (warn) I think you will need it
>> > #!/usr/local/bin/perl -w
>>
>> That -w is rendered useless when you put a comment above the shebang.
>> The shebang must always be the absolute first sequence of characters in
>> the file, prior to any code, comments, or whitespace.
>>
>> Paul Lalli
>>
>
>
> 100% right Paul.
>
> I should have commented after in case Marco directly cut and pasted from
> the text of the message.
>
> Sorry for the bad.
>
> Bill

--
Xiao Zhang Kung Fu
http://www.xzkungfu.com
Community Martial Art Center Evansville, IN