Re: Passing file name /filehandle as argument to a subroutine...

2004-03-27 Thread R. Joseph Newton
urvashi mishra wrote:

 hi;

 i am trying to take input from multiple files
 Various I/P files are specified at command line...

What is an I/P file?  Do you mean an input file?

 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...

 Code is:

 foreach my $file (@ARGV)
 {
 # if it's dead, strip it out
  pass1($file);
  display();

should be:
 pass1($file);
 display();


  print  \n 8next**\n;
 }


 and the function to be called is

 sub pass1

So you are passing one of something?  What is this function meant to achieve?
The name should clearly indicate that.


 {
  my ($file)[EMAIL PROTECTED];

could be:
my $file = shift;
or
my $file = $_[0];
though the form you chose makes sense if you expect later to expand the argument
list.


  print $file;
  open(MIBFH,$file)|| die Error opening the  $file $!\n;

Better not to use the newline at the end.  You get more information if you just:

 open(MIBFH,$file) or die Error opening the  $file$!;
Perl will take care of adding a newline after the error message.

  while(my $line = MIBFH )
  {
 
 }
 close(MIBFH);

 }

 Can anyone help me...!

Probably, if you tell us what the problem is.  You have told us some of what you
want, and shown us the code, but you haven't told us what happens when you run
it.

Joseph


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Passing file name /filehandle as argument to a subroutine...

2004-03-27 Thread R. Joseph Newton
R. Joseph Newton wrote:

 Better not to use the newline at the end.  You get more information if you just:

  open(MIBFH,$file) or die Error opening the  $file$!;

Should be:
open(MIBFH,$file) or die Error opening the  $file: $!;

Sorry,

Joseph


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Passing file name /filehandle as argument to a subroutine...

2004-03-25 Thread John W. Krahn
Urvashi Mishra wrote:
 
 hi;

Hello,

 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...
 
 Code is:
 
 foreach my $file (@ARGV)
 {
  #my $file= shift @ARGV;
  #print $file\n;
  #
  pass1($file);
  display();
  print  \n 8next**\n;
 }
 
 
 and the function to be called is
 
 sub pass1
 {
  my ($file)[EMAIL PROTECTED];
  print $file;
  #MIBFH++;
  open(MIBFH,$file)|| die Error opening the  $file
 $!\n;
  while(my $line = MIBFH )
  {
 
 }
 close(MIBFH);
 
 }
 
 Can anyone help me...!


How about something like this:

while (  ) {  # process files in @ARGV
if ( $. == 1 ) {
print File name: $ARGV\n;
}

# process the contents of the file

if ( eof ) {
close ARGV;  # reset $. for next file
print  \n 8next**\n;
}
}



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Passing file name /filehandle as argument to a subroutine...

2004-03-23 Thread urvashi mishra
hi;
 
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...
 
Code is:
 
foreach my $file (@ARGV)
{
 #my $file= shift @ARGV;
 #print $file\n;
 #
 pass1($file);
 display();
 print  \n 8next**\n;
} 

 
and the function to be called is
 
sub pass1
{
 my ($file)[EMAIL PROTECTED];
 print $file;
 #MIBFH++;
 open(MIBFH,$file)|| die Error opening the  $file
$!\n;
 while(my $line = MIBFH ) 
 {

}
close(MIBFH);
 
}
 
Can anyone help me...!
 
Regs
urvashi

__
Do you Yahoo!?
Yahoo! Finance Tax Center - File online. File on time.
http://taxes.yahoo.com/filing.html

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




RE: Passing file name /filehandle as argument to a subroutine...

2004-03-23 Thread Charles K. Clarkson
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