Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Win32 Opening files

by Dalin (Sexton)
on Jun 29, 2001 at 18:27 UTC ( [id://92635]=perlquestion: print w/replies, xml ) Need Help??

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

Hey all, So, with the help of everyone else I was able to get the file name globbing taken care of. Now I'm running into a problem of not being able to open a file. I have a parameter file that contains the names of two other files with full pathnames. In my code, I try to open the parameter file for reading but die with "bad file descriptor". Here is my code:
sub parse_mail { my $mail = "@_"; open (MAIL,$mail) || die "Cannot open $mail, $!"; @mailf = <MAIL>; chomp (@mailf); close (MAIL) || die "Cannot close $mail, $!"; return @mailf; }
I would appreciate any help that anyone might give. Thanks in advance, Bradley
Where ever there is confusion to be had... I'll be there.

Replies are listed 'Best First'.
Re: Win32 Opening files
by bwana147 (Pilgrim) on Jun 29, 2001 at 18:36 UTC

    @_ contains the list of arguments to your function, and when you interpolate it in a string, you get all your parameters separated by a space (unless you fiddled with some funny variables, but I assume you didn't).

    I suppose you pass your two full pathnames as arguments, so you have to loop on @_:

    sub parse_mail { my @mail; foreach my $mail ( @_ ) { open (MAIL,$mail) || die "Cannot open $mail, $!"; push @mailf, <MAIL>; chomp (@mailf); close (MAIL) || die "Cannot close $mail, $!"; } return @mailf; }

    I also assume you want to concatenate both files. But may be it's better that your sub handles one file at a time and you call it from within a loop...

    --bwana147

      Ok, all you guys rock for all of the help. I changed;
      my $mail = "@_";
      to
      my $mail = @_[0];
      This seems to work because I actually have a foreach loop in the main. It is getting past the open but not the close on the filehandle. I basically get the same message: Cannot close C:/windows/desktop/ tmp/test.f2mail,Bad file descriptor. Where ever there is confusion to be had... I'll be there.
        which should be my $mail = $_[0];

        I suggest you put -w after your path to perl at the top of the file (#!/usr/bin/perl or whatever) which will pick up on stuff like that.

        "Argument is futile - you will be ignorralated!"

      Perhaps it might work better in windows if you break it down into simpler elements, for instance
      sub parse_mail ( my @mail; @mail = @_; foreach my $mail ( @mail ) { open (MAIL,$mail) || die "Cannot open $mail, $!"; push @mailf, <MAIL>; chomp (@mailf); close (MAIL) || die "Cannot close $mail, $!"; } return @mailf; }

      I realize that you have set up a shortcut in the code, but I have found perl works better in Windows if I avoid shortcuts in the code, I don't know why.

      MadraghRua
      yet another biologist hacking perl....

        Whats the deal with this line:
        foreach my $mail ( @mail ) {
        or this :
        my @mail; @mail = @_;
        Why not just say:
        foreach my $mail (@_) {
        and using $mail as the looping element of @mail is kind of silly... I dont know that perl would even let you do that (at least not with strict and warnings).

        --
        Laziness, Impatience, Hubris, and Generosity.

Re: Win32 Opening files (boo)
by boo_radley (Parson) on Jun 29, 2001 at 18:44 UTC
    there's a little PSI::ESP in my answer, so consider yourself warned.
    If @_ is going to be a list of files, then my $mail = "@_"; will create a string that contains all the elements of @_ separated by spaces. It's sort of a shorthand for  join (" ", @_);. so you probably want to run through @_ in a foreach or while loop.
Re: Win32 Opening files
by bikeNomad (Priest) on Jun 29, 2001 at 18:34 UTC
    So what's the filename you're using? Perhaps it's a bad name. Or do you already have too many files open?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2024-04-23 08:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found