문제

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.

도움이 되었습니까?

해결책 3

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;

다른 팁

remove the quote marks around the regexp

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

The easiest way is probably Perl6::Slurp module:

use Perl6::Slurp;
$contents = slurp \*SEQ;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top