urvashi mishra <[EMAIL PROTECTED]> wrote:
:
: i am trying to take input from multiple files....
: Various I/P files are specified at command line...
:
: Can anyone tell me how to pass the file name to a
: routine that opens it for parsing ....
:
: the same function is to be called for all the I/P
: files...
Okay, what's an I/P file?
: Code is:
:
: foreach my $file (@ARGV)
: {
: #my $file= shift @ARGV;
: #print "$file\n";
: #
: &pass1($file);
In modern perl we tend to drop the & in front of
subroutine calls. pass1() is a lousy sub name. Why
not pick something more descriptive?
: &display();
: print " \n ****8next******\n";
: }
:
:
: and the function to be called is
:
: sub pass1
: {
: my ($file)[EMAIL PROTECTED];
: print "$file";
Why is $file in quotes?
: #MIBFH++;
: open(MIBFH,$file)|| die "Error opening the $file
: $!\n";
If you leave the "\n" off the end you'll get more
complete error messages.
: while(my $line = <MIBFH> )
: {
: ....
: }
: close(MIBFH);
:
: }
:
: Can anyone help me...!
What is the problem you are having? This seems to
work fine for me. Here's my test code. ($0 is the
file and path to the current script.)
use strict;
use warnings;
foreach my $file ( $0, $0 ) {
pass1( $file );
# display();
}
sub pass1 {
my( $file ) = @_;
print $file;
open FH, $file or die qq|Error opening the "$file": $!|;
while ( my $line = <FH> ) {
print $line;
last;
}
close FH;
}
__END__
We might localize the file handle to the subroutine so we
don't clobber any open handles:
sub pass1 {
my( $file ) = @_;
print $file;
local *FH;
open FH, $file or die qq|Error opening the "$file": $!|;
while ( my $line = <FH> ) {
print $line;
last;
}
close FH;
}
In recent versions of perl we could use a lexical variable:
sub pass1 {
my( $file ) = @_;
print $file;
open my $fh, $file or die qq|Error opening the "$file": $!|;
while ( my $line = <$fh> ) {
print $line;
last;
}
close $fh;
}
HTH,
Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>