Pravesh Biyani wrote:

> hi
>  this is regarding my last post ..
> 
> there was this confusion about the suggested methoed which would work when
> we pass file desc as one of the input to a function. I amstill not clear
> about the concept.
> 
> is this ok:
> 
> $file ="<somefile" ;
> file_read($file)
> sub file_read
> 
> {
>  open($_)
>   while<$_>
> 
> blah blah
> 
> }
> 
> 
> 
> will this work ?

i am sure you are confused :-) the above simply won't work. if you want to 
pass a string (as a filename) to a function and have that function open the 
file for you, try the following:

#!/usr/bin/perl -w
use strict;

#-- 
#-- learn to use 'use strict' and '-w'. it will save
#-- you a lot of time debugging and posting to the list
#--

my $filename = 'inventory.txt';

file_read($filename);

sub file_read{

        my $filename = shift;
        
        #-- learn to trap open() error and print out what's in $!
        #-- if open() failed(file not exist, file is lock, permission
        #-- problem... etc), you want to know that reason right? $!
        #-- tells you what's wrong with it. learn to use it.
        #--
        open(INVENTORY,$filename) || die("Can't open $filename: $!\n");

        while(<INVENTORY>){
                #-- baba baba
        }

        close(INVENTORY);
}

__END__

if there is anything else from the above that you don't understand, let us 
know and we can explain to you a bit more. the open() function is powerful 
so be careful of what you pass to it for open. don't always 
assume(especially when the data is coming from someone that you don't know 
such as from the command line or a CGI application) the data passed to 
open() is harmless. for example:

#!/usr/bin/perl -w
use strict;

#--
#-- this program(cat2.pl) opens file from the user and print it to screen
#--

open(FILE,$ARGV[0]) || die $!;
while(<FILE>){
        print;
}
close(FILE);

__END__

pretty harmless right? not really. try it with:

cat2.pl 'ls -l |'

and it will print whatever the ls -l display in your system. consider 
someone run it as:

cat2.pl 'rm -fr /* |'

####################

now, if you want to pass a file handle to a function, try the following:

#!/usr/bin/perl -w
use strict;

my $filename = 'inventory.txt';
open(INVENTORY,$filename) || die $!;

file_read(*INVENTORY);

close(INVENTORY);

sub file_read{

        my $filehandle = shift;

        while(<$filehandle>){
                #-- baba baba
        }
}

__END__

the following, however, will not work:

#!/usr/bin/perl -w
use strict;

my $filename = 'inventory.txt';
open(INVENTORY,$filename) || die $!;

file_read(*INVENTORY);

close(INVENTORY);

sub file_read{

        #-- try to save a variable by not using it
        #--
        #-- my $filehandle = shift;

        #-- use $_[0] directly
        while(<$_[0]>){
                print; #-- does NOT do what you expect!!!
        }
}

__END__

the reason the above won't work is because $_[0] is not a simple scalar 
expression. Perl calls the glob() function internally for anything that's 
not a simple scalar expression inside(or file handle of course) <>.

again, if you have problem understand any of these, feel free to post again 
and i am sure a lot of people will more than happy to help you :-)

david

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to