Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

how to stop the file

by dakmatt (Initiate)
on Feb 05, 2002 at 08:05 UTC ( [id://143378]=perlquestion: print w/replies, xml ) Need Help??

dakmatt has asked for the wisdom of the Perl Monks concerning the following question:

hi,i'm writing the below code.but i was stuck in the middle.please help me...
if(open_con(F,"localhost","port")) { $text = "some expression to be send to the server"; syswrite(F,$text,500); open(AUDIO,">$audio_file); $receive = <F>; // instead of sysread(F,$receive,500) while($receive = <F>) { print AUDIO $receive; } close AUDIO; }
the open_con() function is to open connection to the server.that one is functionally ok.the above problem with the above code is that i don't know how to terminate the while loop after it finish writing the into the AUDIO file from streaming <F> . i try to modified like this.
while($receive=<F>) { if($receive eq "\n") { last; } print AUDIO $receive; }
the above code,doesn't work too. so,please give me any suggestion or sample code to stop the streamming <F> and terminate it writing to the AUDIO file.
thank you in advance.
:D

Replies are listed 'Best First'.
Re: how to stop the file
by particle (Vicar) on Feb 05, 2002 at 13:56 UTC
    are you using strict and warnings? i can't tell from your code, but i don't see any my declarations. your example of the while/if/last loop should work. that is, last should exit the while loop. here's my code, it works. the output is below it.

    #!/usr/local/bin/perl -w use strict; $|++; my $r; while($r=<DATA>) { print "hi$r"; if($r eq "\n") { last; } print "bye$r\n"; } __DATA__ 1 2 3 4
    the output:

    C:\WINDOWS\Desktop>perl test_last.pl hi1 bye1 hi2 bye2 hi3 bye3 hi C:\WINDOWS\Desktop>
    are you certain "\n" is the condition you need to stop the loop? or is it EOF? if it's the end of the file you're looking for, i'd suggest something like

    #!/usr/local/bin/perl -w use strict; $|++; while(<DATA>) { print "$_"; } __DATA__ 1 2 3 4
    if you're not using strict, you might want to read up on it. start here, at Use strict warnings and diagnostics or die.

    ~Particle

Re: how to stop the file
by gt8073a (Hermit) on Feb 05, 2002 at 15:56 UTC

    syswrite(F,$text,500);

    are you handling partial write when you do not have 500 bytes of data in $text? that could cause you problems.

    Will perl for money
    JJ Knitis
    (901) 756-7693
    gt8073a@industrialmusic.com

Re: how to stop the file
by screamingeagle (Curate) on Feb 05, 2002 at 08:11 UTC
    you could use the last function , which immediately breaks out of the loop. Keep in mind that (Quoting from the documentation) "last cannot be used to exit a block which returns a value such as eval {}, sub {} or do {}, and should not be used to exit a grep() or map() operation"
      He uses last in his example.

      metadoktor

      "The doktor is in."

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://143378]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-24 23:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found