Questions tagged [filehandle]

A file handle is an abstract indicator for accessing a file. It is retrieved after successfully opening the file using the file name. Afterwards the file handle is used instead of the file name for all file operations.

388 questions
14
votes
1 answer

I can create filehandles to strings in Perl 5, how do I do it in Perl 6?

In Perl 5, I can create a filehandle to a string and read or write from the string as if it were a file. This is great for working with tests or templates. For example: use v5.10; use strict; use warnings; my $text = "A\nB\nC\n"; open(my $fh, '<',…
Christopher Bottoms
  • 10,220
  • 7
  • 44
  • 87
14
votes
2 answers

Prevent strings from being interpreted as a file handle

Perl has the feature that strings named like a file handle are taken to be a filehandle: # let this be some nice class I wrote package Input { sub awesome { ... } } So when we do Input->awesome or extra-careful: 'Input'->awesome, the method…
amon
  • 54,982
  • 2
  • 82
  • 143
13
votes
2 answers

Close all open files in ipython

Sometimes when using ipython you might hit an exception in a function which has opened a file in write mode. This means that the next time you run the function you get a value error, ValueError: The file 'filename' is already opened. Please close…
tdc
  • 7,149
  • 11
  • 38
  • 61
13
votes
1 answer

How do I determine whether a Perl file handle is a read or write handle?

You are given either an IO::File object or a typeglob (\*STDOUT or Symbol::symbol_to_ref("main::FH")); how would you go about determining if it is a read or write handle? The interface cannot be extended to pass this information (I am overriding…
Chas. Owens
  • 61,688
  • 16
  • 123
  • 216
12
votes
5 answers

How can I test if I can write to a filehandle?

I have some subroutines that I call like this myWrite($fileName, \@data). myWrite() opens the file and writes out the data in some way. I want to modify myWrite so that I can call it as above or with a filehandle as the first argument. (The main…
flies
  • 1,912
  • 2
  • 21
  • 35
12
votes
5 answers

Is there a way to access a string as a filehandle in php?

I'm on a server where I'm limited to PHP 5.2.6 which means str_getcsv is not available to me. I'm using, instead fgetcsv which requires "A valid file pointer to a file successfully opened by fopen(), popen(), or fsockopen()." to operate on. My…
Erik
  • 19,600
  • 7
  • 43
  • 75
11
votes
5 answers

Python equivalent of piping file output to gzip in Perl using a pipe

I need to figure out how to write file output to a compressed file in Python, similar to the two-liner below: open ZIPPED, "| gzip -c > zipped.gz"; print ZIPPED "Hello world\n"; In Perl, this uses Unix gzip to compress whatever you print to the…
bu11d0zer
  • 383
  • 3
  • 12
11
votes
5 answers

Why does Programming Perl use local (not my) for filehandles?

When I read through Programming Perl, 2nd Edition, Page 51, something confuses me : sub newopen { my $path = shift; local *FH; #not my! open (FH, $path) || return undef; return *FH; } $fh = newopen('/etc/passwd'); My I know, why…
Cheok Yan Cheng
  • 49,649
  • 117
  • 410
  • 768
11
votes
2 answers

How do I release file system locks after cloning repo via JGit

I am playing around with cloning a remote existing repo with jGit following the guide here: https://github.com/centic9/jgit-cookbook/blob/master/src/main/java/org/dstadler/jgit/porcelain/CloneRemoteRepository.java I'm using CFML for my example: Git…
Brad Wood
  • 3,466
  • 12
  • 20
10
votes
4 answers

Sending data to a program via stdin and ostream. (C++)

I would like to send data from within my C++ program to an external pipeline, like so: FILE* file = popen("my_prog -opt | other_prog", "w"); std::ostream fileStream = some_function(file); fileStream << "some data"; I understand there is no simple,…
Drew
  • 9,858
  • 10
  • 52
  • 88
9
votes
3 answers

What is the difference between writing to STDOUT and a filehandle opened to "/dev/tty"?

What are the differences between this two examples? #!/usr/bin/perl use warnings; use 5.012; my $str = "\x{263a}"; open my $tty, '>:encoding(utf8)', '/dev/tty' or die $!; say $tty $str; close $tty; open $tty, '>:bytes', '/dev/tty' or die $!; say…
sid_com
  • 21,289
  • 23
  • 89
  • 171
8
votes
3 answers

Can I use Text::CSV_XS to parse a csv-format string without writing it to disk?

I am getting a "csv file" from a vendor (using their API), but what they do is just spew the whole thing into their response. It wouldn't be a significant problem except that, of course, some of those pesky humans entered the data and put in…
Paul R N
  • 83
  • 3
8
votes
2 answers

Why does Perl open() documentation use two different FILEHANDLE styles?

The documentation for the open function shows the syntax of open() as: open FILEHANDLE,EXPR open FILEHANDLE,MODE,EXPR open FILEHANDLE,MODE,EXPR,LIST open FILEHANDLE,MODE,REFERENCE open FILEHANDLE Down in the examples they have places where a…
Scooter
  • 5,908
  • 8
  • 32
  • 59
8
votes
7 answers

How can I process a multi line string one line at a time in perl with use strict in place?

I'm trying to figure out the proper PBP approved way to process a multi line string one line at a time. Many Perl coders suggest treating the multi line string as a filehandle, which works fine unless you have "use strict" in your script. Then you…
Kurt W. Leucht
  • 4,529
  • 8
  • 30
  • 45
8
votes
1 answer

Support autovivified filehandle as arguments to Perl XS routine

Question How can I support autovivified filehandle arguments in an XS function? I'm XS-wrapping a C function which returns a file descriptor, and I'd like to present that file descriptor as a perl filehandle argument in the manner of open(). …
pilcrow
  • 51,771
  • 11
  • 83
  • 128
1
2
3
25 26