Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Open Has a New Trick

by Zaxo (Archbishop)
on Oct 02, 2002 at 18:20 UTC ( #202343=perlmeditation: print w/replies, xml ) Need Help??

I was poking around in Perl 5.8.0's core module library, and turned up a new form of open that I hadn't heard about. It happens in lib/5.8.0/{arch}/PerlIO/

Something I've occasionally wanted to do is to attach a file handle to a string as an accessor. That has been possible with earlier perl using IO::Scalar or IO::Stringy, but not all filehandle semantics and conventions were honored. I found that that is native in 5.8.0 with PerlIO, and that the syntax is as simple as could be imagined: open FILEHANDLE, MODE, SCALARREF.

That's it - give open a reference to the string in the file name slot, and it works. The handle may be read, written, or appended. Globals like $/ work as expected.

Here's a sledgehammer-on-peanut example which prints a string in cable-ese (upper case, suppressed spaces, five letter groups):

my $foo= 'Now is the time for all good men to come to the aid of their + country'; { local $\ = " "; local $/ = \5; $foo =~ tr/ //d; open my $fh, '<', \$foo; print uc while <$fh>; }

As an output file:

my $foo; $_ = "She sells sea shells by the sea shore"; { local $\ = $/; open my $fh, '>', \$foo; print $fh $_ for split; } print $foo;

Append, capturing STDERR:

my $oops; open STDERR, '>>', \$oops;

I've only started to explore this trick. I haven't tried flock or seek/tell or $.. I haven't tinkered with truncate or exotic modes.

Here's a challenge. Can you find any limits to file-like behavior? What cool uses can you think of for this?

After Compline,

Replies are listed 'Best First'.
Re: Open Has a New Trick
by rob_au (Abbot) on Oct 02, 2002 at 21:55 UTC
    I found this feature in the 5.8.0rc2 and made comment on it as a means by which to perform temporary file-type operations within memory space in this follow-up comment to my tutorial on Using Temporary Files in Perl.

    Another nifty feature in 5.8.0 to do with the open function and temporary files is the creation of anonymous file handles. For example:

    open( $fh, '+>', undef ) || die $!;

    Where the undefined value is a literal undef statement, rather than an undefined variable. This syntax will open a file handle and where permitted by the file system, unlink the file while holding it open. The result is an anonymous STDIO stream which will be lost when the file handle is closed.

    Note too that this anonymous file handle functionality is also available through the File::Temp module which has been incorporated into the core 5.8.0 distribution.


    perl -e 'print+unpack("N",pack("B32","00000000000000000000000111000110")),"\n"'

Re: Open Has a New Trick
by Felonious (Chaplain) on Oct 02, 2002 at 19:18 UTC
Re: Open Has a New Trick
by princepawn (Parson) on Nov 20, 2002 at 21:12 UTC
    Did you also know that Papa's got a brand new bag?

    Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://202343]
Approved by dws
Front-paged by jarich
[thezip]: Ooops... I lied. I guess Cygwin is back. I'll just do a tail -f instead. Better. Sorry for the noise.
[Corion]: Once more, I'm looking for a sane client-side framework, but I guess these don't exist. Everything I look at either uses a weirdo home-grown templating language (like Angular in all its incarnations) or uses weirdo Javascript incarnations (like ...
[Corion]: ... Inferno.js, which uses ES2015) or uses some horrible amount of Javascript infrastructure before you can even render a single file.
[Corion]: I'd really like to create a dynamic frontend for my Google Keep clone, but so far, all the templating solutions seem to bring their own template language or require me to hand-code everything in (their own flavour of) Javascript. I'd like something ...
[Corion]: ... that can use templates from the HTML (via <script language="text/ template">... my template ...</script> and otherwise gets out of my way. But it seems that is not how the Javascript world is, currently :-/

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (11)
As of 2017-03-27 18:58 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (321 votes). Check out past polls.