How to tell if subroutine param is filehandle

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default How to tell if subroutine param is filehandle

    Hi,

    Is there a way for a subroutine to tell when it got passed a FILEHANDLE?

    Running a quick test suggests a way, which is to check the first char of the result of interpolating the subroutine param as a string. Can anyone say whether this is robust and if not what might be a better approach? Thank you.

    testprintorstring.pl:
    ---------------------

    use warnings;
    use strict;

    {
    my $string = "xyz";
    testprint( "Filehandle", *STDIN );
    testprint( "Stringref", \$string );
    }

    sub testprint {
    my( $descr, $r_data ) = @_;
    print "$descr: $r_data ", ref($r_data), "\n";
    }

    output:
    -------

    D:\MCD\dvl\scripts>perl testprintorstring.pl
    Filehandle: *main::STDIN
    Stringref: SCALAR(0x155a898) SCALAR




    __________________________________________________ ________________
    New! Unlimited Netscape Internet Service.
    Only $9.95 a month -- Sign up today at [url]http://isp.netscape.com/register[/url]
    Act now to get a personalized email address!

    Netscape. Just the Net You Need.
    mcdavis941@netscape.net Guest

  2. Similar Questions and Discussions

    1. closed filehandle
      Hello when I run the code below I get readline() on closed filehandle DATA at practice.plx line 6. #!/usr/bin/perl use warnings; use...
    2. FILEHANDLE to print to nothing
      Howdy, I am benchmarking some stuff that does multiple print staements. What I'd like to do is print to a file handel so ethat the stuff I'm...
    3. filehandle to variable problem
      Hello all, i try to store filehandle in variable (as described in perl cookbook for instance) and i am not able to use the variable afterwards....
    4. first.pl | second.pl with ARGV filehandle ?
      >>>>> "s" == stu7 <stuseven@hotmail.com> writes: s> + perl first.pl | perl second.pl something, something ARGV fh ? s> This is what I've seen...
    5. Using filehandle many times
      On 29 Jun 2003 23:43:42 -0700 cdcer@hongkong.com (ABC) wrote: In such cases, 'use diagnostics' might help you with what exactly you need to...
  3. #2

    Default Re: How to tell if subroutine param is filehandle

    On Feb 1, [email]mcdavis941@netscape.net[/email] said:
    >Is there a way for a subroutine to tell when it got passed a FILEHANDLE?
    You could test to see if its ref() returns "GLOB" or "IO::File" or
    "IO::Handle", etc., but I think it's a nifty idea to do:

    if (defined fileno($arg)) {
    # it's an open filehandle
    }

    --
    Jeff "japhy" Pinyan [email]japhy@pobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
    RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
    <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
    [ I'm looking for programming work. If you like my work, let me know. ]

    Jeff 'Japhy' Pinyan Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139