Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Comment on

( #3333=superdoc: 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/scalar.pm

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,
Zaxo


In reply to Open Has a New Trick by Zaxo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (16)
    As of 2014-11-24 15:37 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (143 votes), past polls