Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: passing file handle to functions

by UnderMine (Friar)
on May 18, 2006 at 06:04 UTC ( [id://550168]=note: print w/replies, xml ) Need Help??


in reply to passing file handle to functions

Why not use a variable for your filehandle?

Example from the perlfunc docs

# process argument list of files along with any includes foreach $file (@ARGV) { process($file, 'fh00'); } sub process { my($filename, $input) = @_; $input++; # this is a string increment unless (open($input, $filename)) { print STDERR "Can't open $filename: $!\n"; return; } local $_; while (<$input>) { # note use of indirection if (/^#include "(.*)"/) { process($1, $input); next; } #... # whatever } } See perliol for detailed info on PerlIO.

Hope it help

UnderMine

Replies are listed 'Best First'.
Re^2: passing file handle to functions
by davorg (Chancellor) on May 18, 2006 at 07:54 UTC
    foreach $file (@ARGV) { process($file, 'fh00'); } sub process { my($filename, $input) = @_; $input++; # this is a string increment unless (open($input, $filename)) { print STDERR "Can't open $filename: $!\n"; return; } local $_; while (<$input>) { # note use of indirection if (/^#include "(.*)"/) { process($1, $input); next; } #... # whatever } }

    Did you try that under "use strict"?

    Can't use string ("fh01") as a symbol ref while "strict refs" in use

    From perldoc open:

    If FILEHANDLE is an undefined scalar variable (or array or hash element) the variable is assigned a reference to a new anonymous filehandle, otherwise if FILEHANDLE is an expression, its value is used as the name of the real filehandle wanted. (This is considered a symbolic reference, so use strict 'refs' should not be in effect.)

    So what you're trying only works with "use strict" if the variable that you use for the filehandle contains an undefined value.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      True. I was quoting directly from perldoc perlfunc examples for open. Which seams different from the perldoc open.

      UnderMine

        Wow. That example should probably just go away from the docs. It's not strict-safe and it doesn't even make sense: the filehandle variable is 'fh01' every time.

        Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
        How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://550168]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-16 22:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found