Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix $/ being interpolated into a regular expression #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ t/prepend_file.t
t/pseudo.t
t/read_dir.t
t/slurp.t
t/split.t
t/stdin.t
t/stringify.t
t/tainted.t
Expand Down
8 changes: 7 additions & 1 deletion lib/File/Slurp.pm
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,13 @@ sub read_file {
if ($want_array || $opts->{array_ref}) {
use re 'taint';
my $sep = $/;
$sep = '\n\n+' if defined $sep && $sep eq '';
if( defined $sep ) {
if( $sep eq '' ) {
$sep = '\n\n+' ;
} else {
$sep = quotemeta $sep;
}
};
# split the buffered content into lines
my @lines = length(${$buf_ref}) ?
${$buf_ref} =~ /(.*?$sep|.+)/sg : ();
Expand Down
29 changes: 29 additions & 0 deletions t/split.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use strict;
use warnings;

use File::Basename ();
use File::Spec ();
use lib File::Spec->catdir(File::Spec->rel2abs(File::Basename::dirname(__FILE__)), 'lib');
use FileSlurpTest qw(temp_file_path);

use File::Slurp qw(slurp write_file);
use Test::More;

plan tests => 1;

my $data = <<TEXT;
line 1{{{{more text
TEXT

my $file = temp_file_path();

$data =~ s!\s*$!!;

write_file($file, $data);

$/ = '{{{{';

my @lines = slurp($file);
is_deeply(\@lines, ['line 1{{{{', 'more text'], 'slurp multiline regex');

unlink $file ;