0

I am having problems doing this:

$contents = do { local $/;  <SEQ> };
$contents =~ "s/\n/ /g";
print $contents;

I want to join all lines of a file into one. However, it doesn't work...

Any idea what's happening with my code?

Thank you.

user2886545
  • 711
  • 2
  • 8
  • 18

3 Answers3

9

remove the quote marks around the regexp

$contents = do { local $/;  <SEQ> };
$contents =~ s/\n/ /g;
print $contents;
Vorsprung
  • 32,923
  • 5
  • 39
  • 63
-1

The easiest way is probably Perl6::Slurp module:

use Perl6::Slurp;
$contents = slurp \*SEQ;
-2

You can use File::Slurp module:

use File::Slurp qw/read_file/;
my $text = read_file( 'filename' ) ;
$text =~ s/\n/ /g;
print $text;

If you don't want to use the module, you can try this:

#!/usr/bin/perl

use strict;
use warnings;

my $text;
open my $fh, "<", "./alarm.pl" or die $!;
{
    local $/;                   # enable localized slurp mode
    $text = <$fh>;
}
close $fh;

$text =~ s/\n/ /g;
print $text;
user4035
  • 22,508
  • 11
  • 59
  • 94