> -----Original Message----- > From: timothy adigun [mailto:2teezp...@gmail.com] > Sent: Thursday, September 22, 2011 2:05 PM > To: Maggs > Cc: beginners@perl.org > Subject: Re: Perl file handling > > Hi Maggs, > > I don't know what you really wants to achieve with your codes below: > >> (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. > > I suppose you want to want to open some files using a subroutine, see > if the > code below helps, and if not you may have to explain what you really > want to > do. > > <codes> > #!/usr/bin/perl -w > use strict; > > open_file(@ARGV); # call the subroutine > > sub open_file{ > foreach (@_){ # iterate on the files to open > open FH,"<",$_ || die "can't open"; # $_ takes the name of the > file to > open > while(<FH>){ chomp; > print $_,"\n"; # $_ is the line you are printing out > } > close FH || die "can't close file"; > } > } > </codes> > Note: You can call the FH, $fh if you like, but then you must use *my* > with > strict in use. You can also use different names for $_, that depends on > you. > Regards, > tim
OP was using trying to use 'open_file' to open one file handle at a time. Arguments were presumably the file handle variable, mode, and file name. Your example is very different. Your code has a precedence problem on the open call. open FH,"<",$_ || die "can't open"; Try this on a file that does not exist and see what happens. You need to either place parentheses around the arguments to open or use the 'or' operator. The '||' operator has higher precedence than the comma. Therefore, it will only die when $_ evaluates to false. Also, you should include the reason for the open failure in the message used by die ($! Or $^E). HTH, Ken -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/