Questions tagged [slurp]

For programming questions about reading a file fully in one operation rather than reading it one line at a time.

In Perl, slurp mode is activated by un-setting a special variable: $/ = undef; after which, reading from a file handle will read the entire file rather than the default of just one line from it.

There is also a File::Slurp Perl module that offers similar functionality of reading the entire contents of a file through a read_file method rather than messing around with magic variables.

Ansible has a similar concept with a built-in slurp module that fully reads files from remote nodes.

39 questions
0
votes
2 answers

Reliable Perl encoding with File::Slurp

I need to replace every occurrence of http:// with // in a file. The file may be (at least) in UTF-8, CP1251, or CP1255. Does the following work? use File::Slurp; my $Text = read_file($File, binmode=>':raw'); $Text =~…
porton
  • 5,214
  • 11
  • 47
  • 95
0
votes
1 answer

Weird perl behavior. Slurp versus an assignment

I ran across something strange in perl that I thought I'd share. I have a text file called "testfile.txt". Here it is.... BLAH BLAH BLAH BLAH BLAH The dollar amount is $2.30 today BLAH BLAH BLAH BLAH BLAH Now I want to extract the 2.30. In my…
Jane Wilkie
  • 1,703
  • 3
  • 25
  • 49
0
votes
2 answers

Extract IP address from a full line after slurping file and using regex to match IP address

I have written the following code to read a file, slurp, identify IP addresses and track the number of occurrences of each address using a hash structure. The problem is that instead of my key being the IP address matched from regex, the key is the…
0
votes
7 answers

How do I read a file's contents into a Perl scalar?

what i am trying to do is get the contents of a file from another server. Since im not in tune with perl, nor know its mods and functions iv'e gone about it this way: my $fileContents; if( $md5Con =~ m/\.php$/g ) { my $ftp =…
Phil Jackson
  • 10,238
  • 23
  • 96
  • 130
0
votes
2 answers

Perl - empty rows while writing CSV from Excel

I want to convert excel-files to csv-files with Perl. For convenience I like to use the module File::Slurp for read/write operations. I need it in a subfunction. While printing out to the screen, the program generates the desired output, the…
royskatt
  • 1,190
  • 2
  • 15
  • 35
-1
votes
4 answers

how to make my json formatted using python

i have json that looks like this: { "message": ".replace(commentRegExp, '')", "report_id": 1961272 }{ "message": ".replace(currDirRegExp, '')", "report_id": 1961269 }{ "message": ".replace(jsSuffixRegExp, '');", "report_id":…
Ranjith N
  • 61
  • 1
  • 6
-1
votes
1 answer

Perl Slurp in Excel csv file

I am parsing an Excel CSV file in OSX El Capitan, the CSV is here The problem is newlines are marked as '\x0d' (CR). 1] I have been able to transform the file with newlines set as 'x0a' (NL) with $> perl -e 'open(fh, "
Nicola Mingotti
  • 860
  • 6
  • 15
-1
votes
1 answer

How to handle error on File::Slurp read_file properly?

I'm using File::Slurp read_file and write_file functions to updated a file content. Now I'm focusing on add error handling to it. For that I tried doing following methods for file that not actually exist. 1) read_file($file) or die("file read…
Yasiru G
  • 6,886
  • 6
  • 23
  • 43
-2
votes
4 answers

Perl - file slurping issue

I'm running the following code: open my $fh, "<", $file; $/ = undef; my $report = <$fh>; $/ = "\n"; close $fh; print("$report\n\n"); $file refers to a text file that looks like this: a 1 b 2 c 3 I ran this code on two different Linux…
kaspnord
  • 1,403
  • 2
  • 18
  • 28
1 2
3