{"id":6998,"date":"2022-12-20T19:34:36","date_gmt":"2022-12-20T22:34:36","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/iowrap-man3\/"},"modified":"2022-12-20T19:34:36","modified_gmt":"2022-12-20T22:34:36","slug":"iowrap-man3","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/iowrap-man3\/","title":{"rendered":"IO::Wrap (man3)"},"content":{"rendered":"<h1 align=\"center\">IO::Wrap<\/h1>\n<p> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#FUNCTIONS\">FUNCTIONS<\/a><br \/> <a href=\"#METHODS\">METHODS<\/a><br \/> <a href=\"#AUTHOR\">AUTHOR<\/a><br \/> <a href=\"#CONTRIBUTORS\">CONTRIBUTORS<\/a><br \/> <a href=\"#COPYRIGHT &#038; LICENSE\">COPYRIGHT &#038; LICENSE<\/a> <\/p>\n<hr>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">IO::Wrap \u2212 Wrap raw filehandles in the IO::Handle interface<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">use strict; <br \/> use warnings; <br \/> use IO::Wrap; <br \/> # this is a fairly senseless use case as IO::Handle already does this. <br \/> my $wrap_fh = IO::Wrap\u2212>new(*STDIN); <br \/> my $line = $wrap_fh\u2212>getline(); <br \/> # Do stuff with any kind of filehandle (including a bare globref), or <br \/> # any kind of blessed object that responds to a print() message. <br \/> # already have a globref? a FileHandle? a scalar filehandle name? <br \/> $wrap_fh = IO::Wrap\u2212>new($some_unknown_thing); <br \/> # At this point, we know we have an IO::Handle\u2212like object! YAY <br \/> $wrap_fh\u2212>print(&#8220;Hey there!&#8221;);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">You can also do this using a convenience wrapper function<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use strict; <br \/> use warnings; <br \/> use IO::Wrap qw(wraphandle); <br \/> # this is a fairly senseless use case as IO::Handle already does this. <br \/> my $wrap_fh = wraphandle(*STDIN); <br \/> my $line = $wrap_fh\u2212>getline(); <br \/> # Do stuff with any kind of filehandle (including a bare globref), or <br \/> # any kind of blessed object that responds to a print() message. <br \/> # already have a globref? a FileHandle? a scalar filehandle name? <br \/> $wrap_fh = wraphandle($some_unknown_thing); <br \/> # At this point, we know we have an IO::Handle\u2212like object! YAY <br \/> $wrap_fh\u2212>print(&#8220;Hey there!&#8221;);<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Let\u2019s say you want to write some code which does I\/O, but you don\u2019t want to force the caller to provide you with a FileHandle or IO::Handle object. You want them to be able to say:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">do_stuff(*STDOUT); <br \/> do_stuff(&#8216;STDERR&#8217;); <br \/> do_stuff($some_FileHandle_object); <br \/> do_stuff($some_IO_Handle_object);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">And even:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">do_stuff($any_object_with_a_print_method);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Sure, one way to do it is to force the caller to use &#8220;tiehandle()&#8221;. But that puts the burden on them. Another way to do it is to use <b>IO::Wrap<\/b>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Clearly, when wrapping a raw external filehandle (like &#8220;*STDOUT&#8221;), I didn\u2019t want to close the file descriptor when the wrapper object is destroyed; the user might not appreciate that! Hence, there\u2019s no &#8220;DESTROY&#8221; method in this class.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">When wrapping a FileHandle object, however, I believe that Perl will invoke the &#8220;FileHandle::DESTROY&#8221; when the last reference goes away, so in that case, the filehandle is closed if the wrapped FileHandle really was the last reference to it.<\/p>\n<h2>FUNCTIONS <a name=\"FUNCTIONS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">IO::Wrap makes the following functions available.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>wraphandle<\/b> <br \/> # wrap a filehandle glob <br \/> my $fh = wraphandle(*STDIN); <br \/> # wrap a raw filehandle glob by name <br \/> $fh = wraphandle(&#8216;STDIN&#8217;); <br \/> # wrap a handle in an object <br \/> $fh = wraphandle(&#8216;Class::HANDLE&#8217;); <br \/> # wrap a blessed FileHandle object <br \/> use FileHandle; <br \/> my $fho = FileHandle\u2212>new(&#8220;\/tmp\/foo.txt&#8221;, &#8220;r&#8221;); <br \/> $fh = wraphandle($fho); <br \/> # wrap any other blessed object that shares IO::Handle&#8217;s interface <br \/> $fh = wraphandle($some_object);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This function is simply a wrapper to the &#8220;new&#8221; in IO::Wrap constructor method.<\/p>\n<h2>METHODS <a name=\"METHODS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">IO::Wrap implements the following methods.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>close<\/b> <br \/> $fh\u2212>close();<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The &#8220;close&#8221; method will attempt to close the system file descriptor. For a more complete description, read &#8220;close&#8221; in perlfunc.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>fileno<\/b> <br \/> my $int = $fh\u2212>fileno();<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The &#8220;fileno&#8221; method returns the file descriptor for the wrapped filehandle. See &#8220;fileno&#8221; in perlfunc for more information.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>getline<\/b> <br \/> my $data = $fh\u2212>getline();<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The &#8220;getline&#8221; method mimics the function by the same name in IO::Handle. It\u2019s like calling &#8220;my $data = <$fh>;&#8221; but only in scalar context.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>getlines<\/b> <br \/> my @data = $fh\u2212>getlines();<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The &#8220;getlines&#8221; method mimics the function by the same name in IO::Handle. It\u2019s like calling &#8220;my @data = <$fh>;&#8221; but only in list context. Calling this method in scalar context will result in a croak.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>new<\/b> <br \/> # wrap a filehandle glob <br \/> my $fh = IO::Wrap\u2212>new(*STDIN); <br \/> # wrap a raw filehandle glob by name <br \/> $fh = IO::Wrap\u2212>new(&#8216;STDIN&#8217;); <br \/> # wrap a handle in an object <br \/> $fh = IO::Wrap\u2212>new(&#8216;Class::HANDLE&#8217;); <br \/> # wrap a blessed FileHandle object <br \/> use FileHandle; <br \/> my $fho = FileHandle\u2212>new(&#8220;\/tmp\/foo.txt&#8221;, &#8220;r&#8221;); <br \/> $fh = IO::Wrap\u2212>new($fho); <br \/> # wrap any other blessed object that shares IO::Handle&#8217;s interface <br \/> $fh = IO::Wrap\u2212>new($some_object);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The &#8220;new&#8221; constructor method takes in a single argument and decides to wrap it or not it based on what it seems to be.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">A raw scalar file handle name, like &#8220;STDOUT&#8221; or &#8220;Class::HANDLE&#8221; can be wrapped, returning an IO::Wrap object instance.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">A raw filehandle glob, like &#8220;*STDOUT&#8221; can also be wrapped, returning an IO::Wrawp object instance.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">A blessed FileHandle object can also be wrapped. This is a special case where an IO::Wrap object instance will only be returned in the case that your FileHandle object doesn\u2019t support the &#8220;read&#8221; method.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Also, any other kind of blessed object that conforms to the IO::Handle interface can be passed in. In this case, you just get back that object.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">In other words, we only wrap it into an IO::Wrap object when what you\u2019ve supplied doesn\u2019t already conform to the IO::Handle interface.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If you get back an IO::Wrap object, it will obey a basic subset of the &#8220;IO::&#8221; interface. It will do so with object <b>methods<\/b>, not <b>operators<\/b>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><i><small>CAVEATS<\/small><\/i><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This module does not allow you to wrap filehandle names which are given as strings that lack the package they were opened in. That is, if a user opens <small>FOO<\/small> in package Foo, they must pass it to you either as &#8220;*FOO&#8221; or as &#8220;Foo::FOO&#8221;. However, &#8220;STDIN&#8221; and friends will work just fine.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>print<\/b> <br \/> $fh\u2212>print(&#8220;Some string&#8221;); <br \/> $fh\u2212>print(&#8220;more&#8221;, &#8221; than one&#8221;, &#8221; string&#8221;);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The &#8220;print&#8221; method will attempt to print a string or list of strings to the filehandle. For a more complete description, read &#8220;print&#8221; in perlfunc.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>read<\/b> <br \/> my $buffer; <br \/> # try to read 30 chars into the buffer starting at the <br \/> # current cursor position. <br \/> my $num_chars_read = $fh\u2212>read($buffer, 30);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The read method attempts to read a number of characters, starting at the filehandle\u2019s current cursor position. It returns the number of characters actually read. See &#8220;read&#8221; in perlfunc for more information.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>seek<\/b> <br \/> use Fcntl qw(:seek); # import the SEEK_CUR, SEEK_SET, SEEK_END constants <br \/> # seek to the position in bytes <br \/> $fh\u2212>seek(0, SEEK_SET); <br \/> # seek to the position in bytes from the current position <br \/> $fh\u2212>seek(22, SEEK_CUR); <br \/> # seek to the EOF plus bytes <br \/> $fh\u2212>seek(0, SEEK_END);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The &#8220;seek&#8221; method will attempt to set the cursor to a given position in bytes for the wrapped file handle. See &#8220;seek&#8221; in perlfunc for more information.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>tell<\/b> <br \/> my $bytes = $fh\u2212>tell();<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The &#8220;tell&#8221; method will attempt to return the current position of the cursor in bytes for the wrapped file handle. See &#8220;tell&#8221; in perlfunc for more information.<\/p>\n<h2>AUTHOR <a name=\"AUTHOR\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Eryq (<i>eryq@zeegee.com<\/i>). President, ZeeGee Software Inc (<i>http:\/\/www.zeegee.com<\/i>).<\/p>\n<h2>CONTRIBUTORS <a name=\"CONTRIBUTORS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Dianne Skoll (<i>dfs@roaringpenguin.com<\/i>).<\/p>\n<h2>COPYRIGHT &#038; LICENSE <a name=\"COPYRIGHT &#038; LICENSE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Copyright (c) 1997 Erik (Eryq) Dorfman, ZeeGee Software, Inc. All rights reserved.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This program is free software; you can redistribute it and\/or modify it under the same terms as Perl itself.<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  IO::Wrap \u2212 Wrap raw filehandles in the IO::Handle interface <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3348,3007],"class_list":["post-6998","post","type-post","status-publish","format-standard","hentry","category-sin-categoria","tag-iowrap","tag-man3"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/6998","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=6998"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/6998\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=6998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=6998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=6998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}