Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

jrw:

You could do something like this:

sub is_FH_a_wrapped_scalar { my $FH = shift; return grep { $_ eq "scalar} } PerlIO::get_layers($FH); }

This uses PerlIO to find the layers of I/O handlers on your filehandle. If one of them is named "scalar", then the filehandle is ultimately wrapping a scalar variable.

Unfortunately, this is the only way I've found to do it, and PerlIO might not be compiled in all versions you use.

$ perl pm_1214805.pl FH is a wrapped scalar, do something else! <read><5></etc/> <sysread><5><SYSTE> <read><5><SYSTE> $ cat pm_1214805.pl #!/usr/bin/perl use strict; use warnings; sub is_fh_a_wrapped_scalar { my $fh = shift; my @layers = PerlIO::get_layers($fh); return grep { $_ eq "scalar" } @layers; } sub dbg { my ($op, $fh, $rc, $scalar) = @_; $rc = "UNDEF" unless defined $rc; print "<$op><$rc><$scalar>\n"; close $fh or die; } sub doit_read { my ($fh) = @_; my $rc = read $fh, my $scalar, 5; dbg "read", $fh, $rc, $scalar; } sub doit_sysread { my ($fh) = @_; if (is_fh_a_wrapped_scalar($fh)) { print "FH is a wrapped scalar, do something else!\n"; } else { my $rc = sysread $fh, my $scalar, 5; dbg "sysread", $fh, $rc, $scalar; } } my $fh; open $fh, "<", \"/etc/passwd" or die; doit_sysread $fh; open $fh, "<", \"/etc/passwd" or die; doit_read $fh; open $fh, "<", "/etc/passwd" or die; doit_sysread $fh; open $fh, "<", "/etc/passwd" or die; doit_read $fh;

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: Distinguishing a filehandle for an in-memory string by roboticus
in thread Distinguishing a filehandle for an in-memory string by jrw

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-25 19:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found