Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Reading from a filehandle in while-loop
Why does this don´t work in my Script?

open (TEST, "</tmp/test.txt");
while (<TEST> ) {
print $_;
# or just
print;
}


Report this thread to moderator Post Follow-up to this message
Old Post
Bastian Angerstein
10-26-04 01:55 PM


Re: Reading from a filehandle in while-loop
Bastian Angerstein wrote:
> Why does this don´t work in my Script?
>
> open (TEST, "</tmp/test.txt");
> while (<TEST> ) {
>   print $_;
>   # or just
>   print;
> }
>
>

Does the file exists and can you read it?

--
Flemming Greve Skovengaard            The killer's breed or the Demon's seed
,
a.k.a Greven, TuxPower                The glamour, the fortune, the pain,
<dsl58893@vip.cybercity.dk>           Go to war again, blood is freedom's st
ain,
4112.38 BogoMIPS                      Don't you pray for my soul anymore.


Report this thread to moderator Post Follow-up to this message
Old Post
Flemming Greve Skovengaard
10-26-04 01:55 PM


Re: Reading from a filehandle in while-loop
On Tue, 26 Oct 2004 11:44:53 +0200, Flemming Greve Skovengaard wrote:

> Bastian Angerstein wrote: 
>
> Does the file exists and can you read it?

Try this instead:

open (TEST, "</tmp/test.txt") or die "can't open file: $!\n";
while (<TEST> ) {
print $_;
# or just
print;
}

You should always test if a file open has succeeded. With as your scripts
get more involved remember to use warnings and strict.
HTH
Chris.


Report this thread to moderator Post Follow-up to this message
Old Post
Chris Cole
10-26-04 01:55 PM


Re: Reading from a filehandle in while-loop
Bastian Angerstein wrote:
> Joop,
> if I use open... or die "$!" i see that the file is opened correctly but n
othing is in $_.
>
>
>
>
> -----Ursprüngliche Nachricht-----
> Von: Flemming Greve Skovengaard [mailto:dsl58893@vip.cybercity.dk]
> Gesendet: Dienstag, 26. Oktober 2004 11:45
> An: beginners@perl.org
> Cc: Bastian Angerstein
> Betreff: Re: Reading from a filehandle in while-loop
>
>
> Bastian Angerstein wrote:
> 
>
>
> Does the file exists and can you read it?
>

Bottompost, please.
Your script works on my machine. Is this you whole script, do you use:

use strict;
use warnings;

in your script and is there any content in /tmp/test.txt?

--
Flemming Greve Skovengaard            The killer's breed or the Demon's seed
,
a.k.a Greven, TuxPower                The glamour, the fortune, the pain,
<dsl58893@vip.cybercity.dk>           Go to war again, blood is freedom's st
ain,
4112.38 BogoMIPS                      Don't you pray for my soul anymore.


Report this thread to moderator Post Follow-up to this message
Old Post
Flemming Greve Skovengaard
10-26-04 01:55 PM


Re: Reading from a filehandle in while-loop
Bastian Angerstein wrote:
> I noticed that while ($test=<TEST> ) works on my system perfectly but while
 (<TEST> ) don´t ... don´t know why...
> should reinstall perl.
>
> Thanks for your help
> Bastian

They should both work, why they don't is beyond me.

Please post on the list, I am *not* a all-seeing, all-knowing Perl guru,
you know, while I can't answer why one work and the other doesn't someone el
se
on the list might.

And again, please bottompost.

--
Flemming Greve Skovengaard           FAITH, n.
a.k.a Greven, TuxPower                   Belief without evidence in what is 
told
<dsl58893@vip.cybercity.dk>              by one who speaks without knowledge
,
4112.38 BogoMIPS                         of things without parallel.


Report this thread to moderator Post Follow-up to this message
Old Post
Flemming Greve Skovengaard
10-26-04 01:55 PM


Re: Reading from a filehandle in while-loop
On Tue, 26 Oct 2004 11:33:24 +0200, Bastian Angerstein
<ang@nmc-m.dtag.de> wrote:
>=20
> Why does this don=B4t work in my Script?
>=20
> open (TEST, "</tmp/test.txt");
> while (<TEST> ) {
>  print $_;
>  # or just
>  print;
> }

You are making the assumption that  '<TEST>' sets '$_' which is not
true.  Oddly, perl makes '<>' set $_, but not <FILE>... dunno why

consider

while( $_ =3D <TEST> ) {
}


>=20
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>=20
>

Report this thread to moderator Post Follow-up to this message
Old Post
David le Blanc
10-26-04 08:56 PM


RE: Reading from a filehandle in while-loop
David le Blanc wrote:
...
> You are making the assumption that  '<TEST>' sets '$_' which is not
> true.  Oddly, perl makes '<>' set $_, but not <FILE>... dunno why

Sorry, but that's just not correct.

while (<TEST> )

DOES set $_, as documented in perldoc perlop under the secion "I/O
Operators"

The OP has some other problem.

Report this thread to moderator Post Follow-up to this message
Old Post
Bob Showalter
10-26-04 08:56 PM


Re: Reading from a filehandle in while-loop
>>>>> "David" == David le Blanc <dmleblanc@gmail.com> writes:

David> You are making the assumption that  '<TEST>' sets '$_' which is not
David> true.  Oddly, perl makes '<>' set $_, but not <FILE>... dunno why

No, that's completely wrong.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training
!

Report this thread to moderator Post Follow-up to this message
Old Post
Randal L. Schwartz
10-27-04 08:55 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:55 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.