{"id":189,"date":"2004-09-24T21:57:00","date_gmt":"2004-09-24T21:57:00","guid":{"rendered":"http:\/\/jclark.org\/weblog\/Programming\/Perl\/scalarIO.html"},"modified":"2008-01-09T22:47:36","modified_gmt":"2008-01-10T02:47:36","slug":"scalario","status":"publish","type":"post","link":"https:\/\/jclark.org\/weblog\/2004\/09\/24\/scalario\/","title":{"rendered":"Howto treat a Perl Scalar like a Filehandle"},"content":{"rendered":"<p>This morning, for the second time in as many weeks, I banged my head against a Perl problem I thought should be simple:  I wanted to open a filehandle against a scalar (or perform some other chicanery) so that if I have a scalar <code>$text<\/code> full of text, I could do this:<\/p>\n<pre><code>while (&lt;$fh&gt;) {\n    #do something with $_\n}<\/code><\/pre>\n<p>Where the filehandle <code>$fh<\/code> would refer to the contents of the scalar <code>$text<\/code>.  This seems like an obvious thing someone may want to do.  While you could just as easily do something like <code>foreach (split \/\\n\/,$text) { #... }<\/code>, I had a situation where I might have data in a scalar or in a file (or even STDIN) and I wanted to treat them all the same.  I expected this would be covered in the [Camel][6] and\/or the [Cookbook][7], but I couldn&#8217;t find any such thing.  I didn&#8217;t have much luck on the web either.  In the end, I took the low road and faked it with a system call and a pipe, since I the script I was working on is infrequently used.  Here&#8217;s that version:<\/p>\n<pre><code>open(FH, qq[echo &quot;$text&quot; |]) or die &quot;Can&#039;t pipe.&quot;;\nwhile (<\/code><\/pre>\n) { #&#8230; }\n\nWhen the same problem came up this morning in a web service I&#8217;m working on, I decided to try researching it again, since I really didn&#8217;t like the pipe solution.  After alot of digging, I came across the perl module [`IO::Scalar`][1], which lets you do exactly what I wanted.  The `IO:Scalar` version of the code looks like this:\n\n    use IO::Scalar;\n    my $fh = new IO::Scalar \\$text;\n    while () { #&#8230; }\n\nMy test app worked nicely on the Unix development server, but then I realized I still had a problem.  For some period of time, this webservice will be running on a Windows2000 web server (don&#8217;t ask).  `IO::Scalar` is not part of the standard Perl distro.  I&#8217;m using ActiveState&#8217;s [ActivePerl][2], which makes installing Modules via `perl -MCPAN -e shell`, well, challenging.  ActiveState has a nice Perl Package Manager; unfortunately I could find no ready-made package for `IO::Scalar`, so I was stuck.  While stumbling around the ActiveState site looking for inspiration, I found `PerlIO::scalar`.  Now I was on to something.\n\n(A moment for a side note here.  I would write far fewer lines of Perl code per hour if not for the fantastic [Perldoc.com][3] maintained by Carlos Ramirez.  It is an absolutely indispensible resource for me.  However, it&#8217;s a bit buggy.  I can&#8217;t get it to let me search the perl 5.8.0 docs, and the 5.8.4 docs appear incomplete (missing standard modules).  After finding the info on ActiveState&#8217;s site, I figured out how to get to it on Perldoc.com.)\n\nAccording the [docs for `PerlIO::scalar`][4]:\n\n&gt; `PerlIO::scalar` only exists to use XSLoader to load C code that provides support for treating a scalar as an &#8220;in memory&#8221; file.\n\nThe docs on the ActiveState site also note that it&#8217;s not necessary to `use PerlIO::Scalar`.  This reduces the code to the following:\n\n    open $fh, &#8220;) { #&#8230; }\n\nExcellent.  Not only is it concise and easy to use, it&#8217;s part of the standard distro.  I&#8217;ve documented this not only for my own future reference, but to add my bit to the [world&#8217;s largest help database][5].  I just hope I wasn&#8217;t being totally obtuse, to later find that this is Camel page 52 material.\n\n[1]: http:\/\/search.cpan.org\/~eryq\/IO-stringy-2.109\/lib\/IO\/Scalar.pm &#8220;IO::Scalar module at the CPAN&#8221;\n[2]: http:\/\/activestate.com\/Products\/ActivePerl\/ &#8220;ActiveState&#8217;s ActivePerl Product Homepage&#8221;\n[3]: http:\/\/perldoc.com &#8220;Web&#8217;s Best Perl Reference&#8221;\n[4]: http:\/\/www.perldoc.com\/perl5.8.0\/lib\/PerlIO\/scalar.html &#8220;PerlIO::scalar module on Perldoc.com&#8221;\n[5]: http:\/\/www.google.com &#8220;Google.com &#8211; the World&#8217;s Largest Help Database&#8221;\n[6]: http:\/\/www.amazon.com\/exec\/obidos\/ASIN\/0596000278\/ref%3Dnosim\/jclarkorg-20\/ &#8220;Programming Perl, 3rd Ed. (Amazon.com)&#8221;\n[7]: http:\/\/www.amazon.com\/exec\/obidos\/ASIN\/0596003137\/ref%3Dnosim\/jclarkorg-20\/ &#8220;Perl Cookbook (Amazon.com)&#8221;","protected":false},"excerpt":{"rendered":"<p>This morning, for the second time in as many weeks, I banged my head against a Perl problem I thought should be simple: I wanted to open a filehandle against a scalar (or perform some other chicanery) so that if I have a scalar $text full of text, I could do this: while () { [&hellip;]<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-189","post","type-post","status-publish","format-standard","hentry","category-perl"],"_links":{"self":[{"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/posts\/189","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/comments?post=189"}],"version-history":[{"count":0,"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/posts\/189\/revisions"}],"wp:attachment":[{"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/media?parent=189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/categories?post=189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/tags?post=189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}