Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Filehandle woes

by count0 (Friar)
on Dec 11, 2001 at 00:55 UTC ( #130773=perlquestion: print w/replies, xml ) Need Help??

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

I have the misfortune of being in the position to maintain some rather poorly written Perl code.

In one package, a filehandle is created using Socket::socket(). This filehandle (called SOCK) is passed to a subroutine in another package like so: Otherpackage::foo(SOCK)

foo() takes that argument as a scalar: sub foo { my $fh = shift; #.... } and goes even further to make a copy of it(?) like so: my $txt = <$fh>

This just seems inherently wrong to me. But it worked just fine when run with perl 5.004_01. When used with a newer version it doesn't.

It seems to me that this may have just happened to run ok by chance.
But before I go about haphazardly rewriting these two modules to use typeglobs or object filehandles, I'd like to know if perhaps there's more to this bit than I'm recognizing... especially in regards to the $txt = <$fh> part.

Replies are listed 'Best First'.
Re: Filehandle woes
by belg4mit (Prior) on Dec 11, 2001 at 01:10 UTC
    my $txt = <$fh> This reads the first line from $fh and assigns it to scoped (my) variable named $txt. there is nothing wrong with that (it is *not* a copy). The rest of it however, is suspect. Something like this might be better

    use Symbol; ... socket($SOCK=gensym(), PF_INET, SOCK_DGRAM, $proto); ... Otherpackage::foo($SOCK) ... package Otherpackage; sub foo { my $fh = shift; }

    perl -p -e "s/(?:\w);([st])/'\$1/mg"

Re: Filehandle woes
by runrig (Abbot) on Dec 11, 2001 at 01:08 UTC
    $txt = <$fh> reads a line from the filehandle, it doesn't make a copy of it. see 'perldoc perlop' under I/O operators. When you are passing a filehandle to a sub, you should generally say, e.g.: foo(\*SOCK), (except on the built in functions which open the fh like open and socket), but nothing you've described sounds all that bad, except the bare 'SOCK' indicates the lack of 'use strict' which is probably bad. Are you trying to make it work with strict and warnings?

    Update: Ok, I've changed my mind. It's probably bad.

Re: Filehandle woes
by c-era (Curate) on Dec 11, 2001 at 01:11 UTC
    I'm not sure if this is it (since I don't have 5.004_01 around), but this is what I think is happening.

    This code works (it's bad for many reasons, but it works):

    open (TEST,"test") || die $!; mysub (TEST); sub mysub { my $fh = shift; print $fh; my $txt = <$fh>; print $txt; }
    as does this
    package mypac; sub mysub { my $fh = shift; print $fh; my $txt = <$fh>; print $txt; } package main; open (TEST,"test") || die $!; mypac::mysub ("main::TEST");
    The line mysub (TEST) is called like mysub ("TEST"). When using a filehandle, you can use a string instead of the filehandle (which is the  my $txt = <$fh>). I'm guessing that perl 5.004_01 had a shared namespace for filehandles, and later versions of perl do not.
Re: Filehandle woes
by count0 (Friar) on Dec 11, 2001 at 02:00 UTC
    For anyone who may have been interested, the problem seemed to lie in using the bareword 'SOCK'.

    After changing all the calls of foo(SOCK) to (using a reference to the fh) foo(\*SOCK), all was fine.
Re: Filehandle woes
by count0 (Friar) on Dec 11, 2001 at 01:07 UTC
    Rather than ammend that post, it's probably easier just to comment on it...

    I'm no longer confused about what the intentions were with $txt = <$fh>. It's clear that it was simply reading from the filehandle (don't you just hate mondays? ;)

    Anyhow, my question still stands about the overall handling of the filehandles.... Specifically this:
    <code> use Otherpackage;
    socket(SOCK, ....); # ... is just a bunch of args

    package Otherpackage;
    sub foo {
      my $fh = shift;

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://130773]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2022-05-18 23:19 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (71 votes). Check out past polls.