Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Text output is on one line

by spoon (Initiate)
on Dec 10, 2002 at 18:50 UTC ( [id://218861]=perlquestion: print w/replies, xml ) Need Help??

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

When entering data in my form the output is readin as one line. It does not seem to accept carriage return. code is as follows:
$text =$data{"Text"}; #Name of Textbox in form $text =~ s/</&lt;/g; $text =~ s/>/&gt;/g;
Subroutine looks like:
if(-f "$bookfile"){ undef $/; open(FILE, "+<$bookfile") || &debug("Oops, I cannot read the bookfile $bookfile: $!"); if($opsys eq "unix"){flock(FILE, 2);} else{binmode(FILE);} $bookfile_contents = <FILE>; $/ = "tr/\n /";
Can any one help!!!

Replies are listed 'Best First'.
Re: Text output is on one line
by ph0enix (Friar) on Dec 10, 2002 at 19:45 UTC

    If you undefine line separator (undef $/;) than whole file will be red as one line ($bookfile_contents = <FILE>;)

    If you want to read from file line by line than use

    if(-f "$bookfile"){ open(FILE, "+<$bookfile") || &debug("Oops, I cannot read the bookfile $bookfile: $!"); if($opsys eq "unix"){flock(FILE, 2);} else{binmode(FILE);} while (my $line = <FILE>) { # do something with one line } }

    What does mean $/ = "tr/\n /";? Your line will end with following text? I don't think so.

    tr/ /
Re: Text output is on one line
by slife (Scribe) on Dec 11, 2002 at 09:01 UTC
    if($opsys eq "unix"){flock(FILE, 2);} else{binmode(FILE);}

    This is a curious construct; the logic appears to be "lock the filehandle on systems described by the identifier 'unix', set binmode on the filehandle on other systems".

    If you think you might need binmode, set it anyway; it's a no-op on systems that don't make the distinction. And try not to mix operations like that within an if {...} else {...}. Future maintenance programmers will not thank you for it.

    Final point; don't use 'magic' numbers, as you do in flock() here, particularly if there's some likelyhood of cross-platform use. Use the constants provided by the Fcntl module instead.

    use Fcnlt qw(:flock); open(FILE, "+<$bookfile") || &debug("Oops, I cannot read the bookfile $bookfile: $!"); if($opsys eq "unix"){flock(FILE, LOCK_EX) # Error-check here.} else { # What do we do on other systems? } binmode FILE;
      Better yet:
      use Fcntl qw(:flock); binmode FILE; eval { flock FILE, LOCK_EX };
      eval will catch the exception caused by using flock where not supported and keep the program from dying. Just checking for $opsys eq "unix" is not sufficient - Perl does have flock on Windows NT f.ex, and there might be pathological cases where a system that identifies itself as unix does not. Catching the exception is a robust way to have it work wherever possible.

      Makeshifts last the longest.

Log In?
Username:
Password:

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

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

    No recent polls found