Re: Perl file handling

2011-09-23 Thread Maggs
On Sep 23, 1:54 am, 2teezp...@gmail.com (timothy adigun) wrote:
 Hello Maggs,

  KS Ken Slater kl...@psu.edu wrote:

 KSOP was using trying to use 'open_file' to open one file handle at a
 time.
 KSArguments were presumably the file handle variable, mode, and  file
 name.
 KSYour example is very different.

 I think 'KS' got the heart of what you really want, and in that light try if
 this following code could help:

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

 my @arr=();
    my %hash_file=(file_handle='fh',file_mode='mode',filename='name');
     foreach(sort keys %hash_file){
        print Enter your $_: ;
          chomp($hash_file{$_}=STDIN);
       push @arr,$hash_file{$_};
     }
   open_file(@arr);  # call the subroutine

   sub open_file{
     no strict refs;  # use this because filehandle is an expression, that
 is it's value
                                # is the real filehandle, which is considered
 a symbolic ref.
                                # check *perldoc -f open* for more info.
          my ($fh,$mode,$name)=@_;
      open ($fh,$mode,$name) || die can't open $!;
        while($fh){ chomp;
            print $_,\n;   # $_ takes the line you are printing out
        }
      close ($fh) || die can't close file: $!;
   }
 /code

 Regards,
 tim



Hello brandon, am trying to create a simple metasearch engine, so i
have a file that has different words (and links) from different search
engines (e.g. google, yahoo), so what am trying to do is open the file
and retrieve the word which equal to the word entered in the search
text box. The next two subroutines read and write from the file, like
this:

sub read_file{
local ($filevar) = @_;
$filevar;
}

sub read_file{
   local($filevar, $line) = @_;
   print $filevar ($line);
}

so that the results from the files are displayed. So far these two
subroutines dont give any error, its just the one line.


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Perl file handling

2011-09-22 Thread Maggs
am trying to open a file but the problem is on the third line
(open...)

sub open_file {
local $/ = undef;
($filevar, $filemode, $filename) = @_;
open ($filevar, $filemode . $filename) || die (Can't open
$filename);
}
everytime i run it i get the error: Can't use string (FILE1) as a
symbol ref while strict refs in use.

Any help please on how to sort this out.


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/