Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Why do I need a space here?

by pogo (Initiate)
on Jun 28, 2002 at 18:53 UTC ( [id://178099]=perlquestion: print w/replies, xml ) Need Help??

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

This prints:
$i="\< HTML\>\n"; print OUTFILE $i;
This doesn't:
$i="\<HTML\>\n"; print OUTFILE $i;
Adding an ASCII space to the double quoted string after the left angle bracket gets the string into OUTFILE, but with the blank. I've got a similar problem with print qq~ ... ~; pax pogo

Replies are listed 'Best First'.
Re: Why do I need a space here?
by sparkyichi (Deacon) on Jun 28, 2002 at 19:00 UTC
    Try:
    $i = "<HTML>\n"; print OUTFILE $i;
    This has worked for me in the past. I don't think you need the back slash for that. You could also
    $i = '<HTML>'; print OUTFILE "$i\n";
    Sparky
    FMTEYEWTK
Re: Why do I need a space here?
by robobunny (Friar) on Jun 28, 2002 at 19:02 UTC
    well i don't know what is causing your error, (it seems to work for me, 5.6.1 on linux), but you don't need to escape those unless you want literal \'s in your file. in that case you need to escape the \'s:
    $i = "\\<HTML\\>\n";
Re: Why do I need a space here?
by Sifmole (Chaplain) on Jun 28, 2002 at 19:11 UTC
    Try adding $|=1; to turn off buffering, just a guess -- otherwise I have to say what the others do " can not reproduce your problem."
Re: Why do I need a space here?
by flocto (Pilgrim) on Jun 28, 2002 at 19:00 UTC
    Sorry, but I can not reproduce your problem:
    octo@leeloo:~ $ perl -we '$i="\< HTML\>\n"; print STDOUT $i;'
    < HTML>
    octo@leeloo:~ $ perl -we '$i="\<HTML\>\n"; print STDOUT $i;'
    <HTML>

    -octo-

    Update: Forgot the "not".. Sorry..
Re: Why do I need a space here?
by bronto (Priest) on Jun 28, 2002 at 21:00 UTC

    You don't need to escape the <. If you fear that, somewhere, somehow, writing code weirdly, you could confuse Perl and make it believe that you a referring to a filehandle, you could use other tricks, like:

    print SOMEWHERE '<'."HTML>\n" ;

    but you shouldn't need that. In fact, Perl is often smarter than you expect :-)

    Ciao!
    --bronto

    # Another Perl edition of a song:
    # The End, by The Beatles
    END {
      $you->take($love) eq $you->made($love) ;
    }

Re: Why do I need a space here?
by pogo (Initiate) on Jun 28, 2002 at 19:25 UTC
    Thanks to all for their offerings, but I am yet joyless. I have tried the first four suggestions here...They work for you but not for me. Perhaps a tad more info might help our mutual enlightenment. Linux 2.4.3-20SMP (dual cpu), Mandrake 8.0, Perl 5.6.0 The file begins with
    #!/usr/bin/perl -w use strict; use Getopt::Long; use File::Path;
    and the var i$ is
    my($i);
    although I can reproduce the problem without i$ as in:
    print OUTFILE "\< \/HTML\>\n"; print OUTFILE "\<\/HTML\>\n";
    where the first case prints and the second doesn't. Manifestly, this should work as you have all suggested, examples abound. Any other suggestions? pax pogo

      For debugging purposes, would you disable output buffering with $|++ near the beginning of your script, then try printing to STDOUT instead of a filehandle?

      Thanks,
      _______________
      D a m n D i r t y A p e
      Home Node | Email
      It looks like your script might be pretty long. Please try removing a bunch of stuff from it so that you can reduce your script to a simple test case. You may figure out your problem between now and then, but if you end up with a very small script that still exhibits this problem, please post it in this thread.

      What you're describing doesn't make a lot of sense, unfortunately. I suspect there's something else going on. There should be no reason a space in this fashion should affect whether or not your output gets written.

      Be sure, though, that if you're writing to a file that you're checking the file's contents only after your script closes the filehandle. Buffering may delay the actual writing of data to a file until it's closed.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-19 12:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found