Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

$_[0] as a filehandle

by jey (Pilgrim)
on Oct 29, 2003 at 01:55 UTC ( [id://302878]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

This is Perl 5.004_04 for irix-n32

Does anyone know why $_[0] cannot be used as a filehandle?

In the example below myFunc doesn't work whereas myFonc does, do you know why?

#!/usr/bin/perl -w open FH, "<$myFile" or die "Can't open $myFile: $!\n"; &myFunc(\*FH); &myFonc(\*FH); sub myFunc { while (<$_&#91;0&#93;>) { #do something } } sub myFonc { my $handle = shift; while(<$handle>) { # do someything } }

thanks a lot

--
jey

Replies are listed 'Best First'.
Re: $_[0] as a filehandle
by sauoq (Abbot) on Oct 29, 2003 at 02:07 UTC

    You can't use subscripts in a variable used as a filehandle. This applies to the diamond operator as well as print statements. I.e. print $_[0] "foo"; won't work either.

    Update: Here's the relevant bit from perldoc perlop:

    If what's within the angle brackets is neither a filehandle nor a simple scalar variable containing a filehandle name, typeglob, or typeglob reference, it is interpreted as a filename pattern to be globbed, and either a list of filenames or the next filename in the list is returned, depending on context. This distinction is determined on syntactic grounds alone. That means "<$x>" is always a readline() from an indirect handle, but "<$hash{key}>" is always a glob(). That's because $x is a simple scalar variable, but "$hash{key}" is not--it's a hash element.

    By the way, please test your example code before pasting it. Yours had several errors unrelated to the issue you are seeing.

    -sauoq
    "My two cents aren't worth a dime.";
    

      thank you very much, that's what I needed...

      --
      jey
Re: $_[0] as a filehandle
by dpuu (Chaplain) on Oct 29, 2003 at 02:49 UTC
    Other posters have answered why it doesn't work. But if you want to make it work:
    use FileHandle; sub in { $_[0]->getline } sub out { $_[0]->print(@_[1..$#_]) } out \*STDOUT, in \*STDIN;
    --Dave.
Re: $_[0] as a filehandle
by Zaxo (Archbishop) on Oct 29, 2003 at 02:09 UTC

    MyFonc() doesn't do anything with its argument. The split call acts on $_, not @_. Your MyFonc might take whatever it gets from $_ as a glob, giving a list of file names to iterate over.

    MyFunc() will work if you shift in the glob reference first. When MyFonc gets called, *FH is at eof. In between the two, call seek

    myFunc(\*FH); seek FH, 0, 0; myFonc(\*FH); # the repaired one :)

    After Compline,
    Zaxo

Re: $_[0] as a filehandle
by pg (Canon) on Oct 29, 2003 at 02:09 UTC

    I guess you mean shift, not split. (Update: the original post has now been updated. It was a split in stead of a shift.)

      Sorry for the split instead of the shift...

      --
      jey
Re: $_[0] as a filehandle
by batkins (Chaplain) on Oct 29, 2003 at 02:11 UTC
    There are a few reasons. First of all, you're using a reference to a glob as a filehandle - that's bound not to work. Also I'm not really sure what you're doing with split in myFonc; you might want to rethink that code. I would advise using lexically scoped filehandles. Something like this:
    my $fh; open $fh, "<$myFile" or die "blah"; myFunc($fh); sub myFunc { my $fh = shift; while(<$fh>) { # do something } }
    This works.

    The computer can't tell you the emotional story. It can give you the exact mathematical design, but what's missing is the eyebrows. - Frank Zappa
      First of all, you're using a reference to a glob as a filehandle - that's bound not to work.

      Uh... "Bound not to work"?

      perl -le '$x = \*STDOUT; print $x "Bzzzt."'

      That used to be the "accepted" way of passing handles around... :-)

      -sauoq
      "My two cents aren't worth a dime.";
      
        Yes, you're right. I should have tested it out before making that claim. :) I still prefer lexically-scoped filehandles whenever possible, but thanks for pointing out my mistake.

        The computer can't tell you the emotional story. It can give you the exact mathematical design, but what's missing is the eyebrows. - Frank Zappa

      On the contrary, it bounds to work. As sauoq pointed out in his 1st reply, the problem is on the receiving side, not the passing side.

      I tested, his Fonc actually worked, but not Func. You must have noticed that it passes the handler to them in exactly the same way, but Fonc and Func receive the parameter differently.

Log In?
Username:
Password:

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

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

    No recent polls found