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
131
votes
18 answers

In Perl, how can I read an entire file into a string?

I'm trying to open an .html file as one big long string. This is what I've got: open(FILE, 'index.html') or die "Can't read file 'filename' [$!]\n"; $document = ; close (FILE); print $document; which results in:
goddamnyouryan
  • 6,854
  • 15
  • 56
  • 105
42
votes
3 answers

Why is "slurping" a file not a good practice?

Why is "slurping" a file not a good practice for normal text-file I/O, and when is it useful? For example, why shouldn't I use these? File.read('/path/to/text.txt').lines.each do |line| # do something with a…
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
23
votes
5 answers

How to set up a robot.txt which only allows the default page of a site

Say I have a site on http://example.com. I would really like allowing bots to see the home page, but any other page need to blocked as it is pointless to spider. In other words http://example.com & http://example.com/ should be allowed, but…
Boaz
  • 25,331
  • 21
  • 69
  • 77
21
votes
2 answers

Make a parent div webkit-filter not affect children

I'm using a very fancy webkit filter to make background-images grayscale, and on hover over the images become color. Here's the filter filter: none; -webkit-filter: grayscale(0); transition: opacity .3s ease-in-out; -moz-transition: opacity .3s…
little tiny man
  • 789
  • 4
  • 10
  • 26
17
votes
3 answers

Read file into variable in Perl

Possible Duplicate: What is the best way to slurp a file into a string in Perl? Is this code a good way to read the contents of a file into a variable in Perl? It works, but I'm curious if there is a better practice I should be using. open INPUT,…
itzy
  • 11,275
  • 15
  • 63
  • 96
11
votes
1 answer

clojure - slurping files relative to project

I have a project created using leningen in which I've saved clj files in the src/some_project_name directory (alongside the autogenerated core.clj file). Also saved with these clj files are text files which I want to slurp from the clj files they…
Solaxun
  • 2,732
  • 1
  • 22
  • 41
6
votes
1 answer

Why can I slurp a file by assigning the result of angle brackets to the input record separator?

I know I can slurp a file by setting the input record separator ($/) to an undefined value, like open my $fh, '<', $filename or die "Cannot read from $file: $!"; my $contents = do { local $/; <$fh> }; But recently I came across a very similar but…
jja
  • 2,058
  • 14
  • 27
4
votes
2 answers

Slurping http://foobar.mp3 that redirects to http://fizzbar.mp3 in Clojure

I am trying to programatically download mp3 files from this rss feed. When I open an url such as: http://menlochurch.podbean.com/mf/feed/5gv2gb/170219_jortberg.mp3 it redirects to an url like:…
Adam Lee
  • 1,784
  • 1
  • 11
  • 15
4
votes
2 answers

Parsing a groovy file in python

I have a groovy config file that I want to append data too. It would be easier to gather data using python that I want to add but I couldn't find a corresponding ConfigSlurper module in python and there is no straightforward way I saw to be able to…
Ben John
  • 53
  • 1
  • 4
4
votes
2 answers

Lua one-liner to read entire file?

Is there a trick to slurp a file with just one line of code? ("to slup" = to read entire file into a string.) Usually I do the following: local f = io.open("/path/to/file") local s = f:read("*a") f:close() But I wonder if there's a shorter way. I…
Niccolo M.
  • 3,363
  • 2
  • 22
  • 39
3
votes
2 answers

slurping /proc/cpuinfo with clojure

(Clojure newbie) On my linux machine, slurping /proc/cpuinfo raises an error: user=> (slurp "/proc/cpuinfo") java.io.IOException: Invalid argument (NO_SOURCE_FILE:0) Anybody knows why that is? (is the /proc filesystem some kind of second-class…
Rom1
  • 3,167
  • 2
  • 22
  • 39
2
votes
3 answers

Can one concatenate two Perl scripts which use different input record separators?

Two Perl scripts, using different input record separators, work together to convert a LaTeX file into something easily searched for human-readable phrases and sentences. Of course, they could be wrapped together by a single shell script. But I am…
Jacob Wegelin
  • 1,304
  • 11
  • 16
2
votes
2 answers

Ansible slurp module fails with a variable

When I use an Ansible variable with the src option of the slurp module, the slurp module fails. I'm trying to build an Ansible playbook to copy the SSH public key from each node in the group to every other node in the group. I cannot use the Ansible…
awrobinson
  • 765
  • 2
  • 8
  • 11
2
votes
3 answers

How does the Perl's Slurp module work?

I had a look at the source of Slurp and I would love to understand how does slurp() work: sub slurp { local( $/, @ARGV ) = ( wantarray ? $/ : undef, @_ ); return ; } Where is the file even opened?
David B
  • 29,258
  • 50
  • 133
  • 186
2
votes
1 answer

Multiple clojure-liberator decisions reads request body

I have one defresource, that is supposed to take POST-requests, validate request body in :malformed-decision, save the body to database in :post!-decision and return the saved body in :handle-created. (defn parse-project [context] (json/read-str …
samu
  • 1,936
  • 4
  • 22
  • 26
1
2 3