Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

open(INFO, $file) - open MORE THAN 1 File ? 1

Status
Not open for further replies.

polka4ever

Technical User
Jan 25, 2006
42
US
Hello, I would like to use the open() function to open several files. I tried doing it this way:

$file = 'test*.file'; # Name the file
open(INFO, $file) or die "Can't open input file: $!"; # Open the file

But this isn't working.

The files are named:

test.file, test1.file, test2.file and test3.file.

The error message i get is can't open input file. No such file or directory.

What am i doing wrong here?

Thank you & hope you all had a good weekend.

~Polka
Detroit, MI, USA
 
You need to open each file separately. Otherwise, how is the program to know which file you want to read? You can do that with something like:
[tt]
for(<test*.file>)
{
open(INFO,$_);
...
close INFO;
}
[/tt]
 
It's because the open operator doesn't accept any wildcards in the filename. It will try to open the file with constant name 'test*.file' . Something like the following might be what you're looking for (untested):

Code:
use strict;
use warnings;

my( @handles, $fh );
my $err = 0;
foreach ( glob("test?.file") ) {

    # Open file for writing
    open $fh, ">", $_ or $err++, next;
    push @handles, $fh;
}

$err and warn "$0: failed to open $err file(s)!\n";

foreach $fh ( @handles ) {

    # Write to filehandle $fh
    print $fh "foobar";

    close $fh or die "$0: close error : $!\n"
}
 
Very cool, this is very helpful and I will give it a try. Another quick question - what if I wanted to be able to sort of Prompt for the file name - or have the perl script open the file from STDIN? Is that a simple thing to do?

Thanks for you help so much!

~polka girl
 
Code:
my $file_to_open;
$file_to_open = $ARGV[0] or do {
    print "Enter filename: ";
    chomp( $file_to_open = <STDIN> );
};

print "$file_to_open\n";
 
Knights, Thank you! I have a question for you though. If i use the above code for <STDIN> - when i do the "for" loop - will I do:

for (<$file_to_open>){
...
}

I'm trying to look up the (< ... >) thing - but I can't seem to determine what that means ...

Or will I just do for ($file_to_open){
....
}

I tried both ways - and getting a syntax error.
 
OK - i was narrowing this down a bit - and I have to say - I don't understand why I would need a semi-colon after the squiggly bracket for this:

$file_to_open = $ARGV[0] or do {
print "Enter filename: ";
chomp( $file_to_open = <STDIN> );
};

that last squiggly there - why do i need a semi colon after it???? I can see that i obviously do (that's the only way it works) but WHY?

~p4e
 
I assume it's because a do{} block can be embedded inside of other blocks, so perl has to know where the 'do' block ends. It's the same with:

eval {...};
sub {...};

and maybe some others. They are similar to functions, even though they are not functions. Also, they are called curly brackets.
 
Kevin is right, 'perldoc -f do' for more help.

Also, the <> operator can be used to read data (on default: a line) from an open filehandle. STDIN is a "fileglob" (I think, might aswell forget about that) and is always open by default. Any reads from it will read input from the terminal (by default). Anything written to STDOUT will be printed on the terminal (by default). So, to read from a filehandle (e.g. STDIN), you use
Code:
my $var = <STDIN>;
using
Code:
foreach (<STDIN>) { ... }
will iterate over all "lines" returned by <STDIN>.
Code:
while (<STDIN>) { ... }
will basically do the same, but will end when <STDIN> returns non-true (e.g. end of file).

The filehandle itself can be opened and closed by the (surprise) open and close statement.

Code:
open my $filehandle, "<", $file_to_open or die "$0: $!\n";
while (<$filehandle>) {
    print "$file_to_open: $_\n";
}
close $filehandle or die "$0: $!\n";
Hope it helped you a bit.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top