exact print

Discussion in 'Perl Misc' started by George Mpouras, Feb 4, 2013.

  1. I need to print a text to file from within the script. The printed text must
    be the same as the script text.
    I do not want to escape every single character of the text, it should be
    "pass through". So far nothing worked.
    A small script that expose the problem, any help is wellcome.

    open FILE, '>test.txt' or die "$^E\n";
    print FILE q~
    \
    \\
    \\\
    ~;
    close FILE;
     
    George Mpouras, Feb 4, 2013
    #1
    1. Advertising

  2. George Mpouras

    Guest

    open FILE, '>test.txt' or die "$^E\n";
    print FILE <<'F';
    \
    \\
    \\\
    F
    close FILE;
     
    , Feb 4, 2013
    #2
    1. Advertising

  3. not good
     
    George Mpouras, Feb 4, 2013
    #3
  4. At linux it is ok. At windows does not behave as should ...
     
    George Mpouras, Feb 4, 2013
    #4
  5. George Mpouras

    bobmct Guest

    On Mon, 04 Feb 2013 18:02:45 +0000, Henry Law <>
    wrote:

    >On 04/02/13 15:18, George Mpouras wrote:
    >> At linux it is ok. At windows does not behave as should ...

    >
    >Again, forgive me George: you've much more experience than I, but I
    >moved my test program to a Windows image and got this:
    >
    >C:\WIP>type tryout.pl
    >use strict;
    >use warnings;
    >use 5.010;
    >
    >use Data::Dumper;
    >
    >open FILE, '>test.txt' or die "$^E\n"
    >print FILE <<'F';
    >\
    >\\
    >\\\
    >F
    >close FILE;
    >
    >C:\WIP>type test.txt
    >\
    >\\
    >\\\
    >
    >C:\WIP>
    >
    >I was initially suspicious of the apparent extra return at the end of
    >the file but I looked at test.txt with an editor (inside Win) and there
    >is only one. The Win shell "type" command seems to add another. So the
    >output is the same as the Linux version.
    >
    >What am I missing?


    ANother thing you could try is printing with FORMATs. The output will
    be EXACTLY as described on the format. I've used this technique to
    create fixed format files for transferral to dependent agencies with
    excellent results. Runs the same on *nix or *doze.
     
    bobmct, Feb 5, 2013
    #5
  6. there is a strange behaviour of activestate petl (5.16) at windows

    print FILE q~
    \
    \\
    \\\
    ~;

    prints


    \
    \
    \\



    print FILE<<stop_printing;
    \
    \\
    \\\
    stop_printing

    prints

    \
    \
     
    George Mpouras, Feb 5, 2013
    #6
  7. "George Mpouras"
    <>
    writes:
    > there is a strange behaviour of activestate petl (5.16) at windows
    >
    > print FILE q~
    > \
    > \\
    > \\\
    > ~;
    >
    > prints
    >
    >
    > \
    > \
    > \\
    >
    >
    >
    > print FILE<<stop_printing;
    > \
    > \\
    > \\\
    > stop_printing
    >
    > prints
    >
    > \
    > \


    This is not really strange: A here-document whose delimiter is not quoted is
    essentially a "-quoted string. The first \ escapes the newline right
    after it. The \\-sequence in the seond line cause a \ to be
    printed. The third line results in another \ because of the \\ and the
    trailing newline is again escaped (which is a no-op).
     
    Rainer Weikusat, Feb 6, 2013
    #7
  8. if so why windows behaviour is completely different than linux ?
     
    George Mpouras, Feb 6, 2013
    #8
  9. "George Mpouras" <>
    writes:
    > if so why windows behaviour is completely different than linux ?


    When running your two code examples on 'Linux', I get the exact same
    output you posted.
     
    Rainer Weikusat, Feb 6, 2013
    #9
  10. try to run them on a windows box and compare the results between windows and
    linux
     
    George Mpouras, Feb 6, 2013
    #10
  11. "George Mpouras" <>
    writes:
    > try to run them on a windows box and compare the results between
    > windows and linux


    I explained the phenomenon you described for 'Linux'. I have no idea
    about the behaviours on Windows and - frankly - 'something weird
    happening on Windows', isn't that sort-of a tautology? :->
     
    Rainer Weikusat, Feb 6, 2013
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J
    Replies:
    1
    Views:
    305
    Kevin Spencer
    Aug 20, 2003
  2. wweric
    Replies:
    1
    Views:
    345
  3. Guest
    Replies:
    11
    Views:
    3,210
    Peter Seaman
    Dec 19, 2003
  4. keto
    Replies:
    0
    Views:
    1,021
  5. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    387
    David Cournapeau
    Dec 30, 2008
Loading...

Share This Page