Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Open file issues

by davorg (Chancellor)
on Aug 18, 2004 at 09:55 UTC ( [id://383909]=note: print w/replies, xml ) Need Help??


in reply to Open file issues

If you were running with "use warnings" then Perl would have told you what the problem is. Filehandles aren't first-class variables in Perl so you can't just return a filehandle from a function and assign it to a variable like that. You're actually using the same filehandle (FILE) for both files - which is why you can't use it after you've closed the first filehandle.

You can get round this by using lexical filehandles.

sub openFile { my ($file, $accessMode) = @_; my $err; $accessMode = $MODE_READ unless defined $accessMode; if (exists($ACCESS_MODES{$accessMode})) { if (open(my $fh, "$ACCESS_MODES{$accessMode}$file")) { return $fh; } else { $err = $!; if (defined($returnOnError)) { return; } &throwGenError("Can't open $file: $err"); # throw exception } } else { &throwGenError("In openFile: Wrong access mode &accessMode"); } return; }

Some of your logic is a bit scary, so I haven't changed too much. You might need to check it carefully.

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

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

Replies are listed 'Best First'.
Re^2: Open file issues
by hotshot (Prior) on Aug 18, 2004 at 11:07 UTC
    Some of your logic is a bit scary, so I haven't changed too much. You might need to check it carefully. Can you explain what you ment?

    And thanks for your pre answer

      Oh, I just mean that there are a few globals which make me a bit uncomfortable and your error handling is (to my eyes) a bit bizarre. The more "Perlish" approach would be to return a false value for smaller errors and to just die on fatal stuff.

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

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

        Got you, but it's just because I didn't paste most of my code, just snipts.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-25 22:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found