Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Distinguishing a filehandle for an in-memory string

by jrw (Monk)
on May 17, 2018 at 23:42 UTC ( [id://1214805]=perlquestion: print w/replies, xml ) Need Help??

jrw has asked for the wisdom of the Perl Monks concerning the following question:

I have noticed that many file IO operations, such as read(), work on in-memory string filehandles, but sysread() doesn't. So, how can I tell if someone has passed me filehandle to such a thing, so I can work around this limitation?

#!/usr/bin/perl use strict; use warnings; 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) = @_; 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;

Output:

<sysread><UNDEF><> <read><5></etc/> <sysread><5><jrw32> <read><5><jrw32>

Replies are listed 'Best First'.
Re: Distinguishing a filehandle for an in-memory string
by ikegami (Patriarch) on May 18, 2018 at 03:34 UTC

    What you want to know is whether it's a system file handle or not. You can use fileno for that. It returns -1 for file handles that aren't system file handles (such as the in-memory string handles).

      I noticed this, but I saw somewhere in a comment or in some piece of documentation that it says something like "currently returns -1", which made me think that that is not a reliable way to check. But maybe it's as reliable as checking for "scalar" in the list of IO layers?

        Checking for an in-memory handle is not what you want to check for. It's only one kind of Perl file handle that's not a system file handle.

Re: Distinguishing a filehandle for an in-memory string
by roboticus (Chancellor) on May 18, 2018 at 01:26 UTC

    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.

      Thanks, I'll check out how get_layers() is implemented. If I can figure out how to unwrap it myself (in Pure Perl), then I will use that. I tried looking at it today for a bit, but couldn't figure out how to unpeel what the $fh typeglob contained.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1214805]
Approved by Paladin
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (7)
As of 2024-04-18 09:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found