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

Dear Monks

I wrote the following test program to cat 2 files (at some point I will write a module that can (also) concatenate binary files)
#! /usr/bin/perl -lw use strict ; use File::Cat; open OUT,">o3" ; cat ('o1', *OUT ) or die "Can't cat o1: $!"; cat ('o2', *OUT ) or die "Can't cat o2: $!"; close OUT ;
When I do a more on file o3 I see
this line is from o1 this line is from o2
When I remove the -l option on the first line I get
this line is from o1 this line is from o2
So the empty lines are introduced by this option, and is not from the files.
To me this looks like a bug, but I have to add that when I do say things like this its probably a very important feature!
Anyway, is there a way I can turn this option of for a specific code block or for a whole module ?

Thnx LuCa

Replies are listed 'Best First'.
Re: problem with File::Cat
by lidden (Curate) on Apr 24, 2007 at 12:14 UTC
    Yes it is a nice feature see "perldoc perlrun"

    What did you expect the -l option to do when you added it?

      Or, from the man page:

      It has two separate effects. First, it automatically chomps $/ (the input record separator) when used with -n or -p. Second, it assigns "$\" (the output record separator) to have the value of octnum so that any print statements will have that separator added back on. If octnum is omitted, sets "$\" to the current value of $/.

      Or, in the vernacular, if you include the -l option, it will add an end-of-line to each print.

      --
      tbone1, YAPS (Yet Another Perl Schlub)
      And remember, if he succeeds, so what.
      - Chick McGee

      Don't get me wrong, the -l is very nice, but not when you cat files (especially not with binary files)!
      I cannot think of a situation in which this kind of behavior can be useful!!

      Anyway from the suggested docs the answer to my problems was simple
      { local $\ ; # do 'cat' stuff here }
      Thanks a lot
      LuCa

      UPDATE: I checked the code from the cpan module File::Cat and I would suggest to change the following
      while (<FILE>) { print $handle $_; }
      into
      while (<FILE>) { printf $handle $_; }
      (not tested)
        You definitely do not want to use printf like that!   The second argument in your example is the format string so any % characters in it will be interpreted by printf.