in reply to File read and strip

my $file=<>;
Will set $file to the first line (including the newline) read from either STDIN or the file specified on the command line. This is almost certainly not what you want.

You probably want

my $file = shift @ARGV; # get first argument my $nfile = shift @ARGV; # get next argument
Which, outside of subroutines can be abbreviated to
my $file = shift; my $nfile = shift;
or you can use
my ($file,$nfile) = @ARGV;
By the way, it's always a good idea to check open() for errors:
open FILE,">$file" or die "Can't open $file: $!"
update: there are other errors in your code, too. Note that grep() returns a LIST, for one.

Replies are listed 'Best First'.
Re^2: File read and strip
by Andrew_Levenson (Hermit) on Nov 14, 2006 at 19:56 UTC
    Is returning a list bad? What does that mean in relation to what i'm trying to do?

    (I don't mean to be obnoxious, i'm trying to make it easier by not just saying "I don't get it.")
Re^2: File read and strip
by Andrew_Levenson (Hermit) on Nov 14, 2006 at 19:54 UTC
    I'm sorry, but... what?
    The files are user-input, so the $file=<> has worked fine for me in the past.

    Sorry, but I have no clue what else you said besides that. :(
    What does shift @ARGV do? What is it calling from? what does the $! in the die function do?

    Sorry if these are stupid questions, and thanks.
      shift @ARGV removes the first element from @ARGV and returns it. @ARGV contains all the arguments provided to your program.

      I was asssuming you wanted to call your program like this:

      my_program inputfile outputfile
      anyway,

      my $whatever = <>;
      will read a single line from the file(s) as specified in @ARGV. in other words, $whatever will not be the filename but the next (or first) line of the file you passed as an argument. if you don't pass any arguments <> will read from STDIN, which means it will usually wait for you to enter a line. See perlop (quoted below:) Note that lines read usually end in a newline character ("\n") while filenames usually do not.
      while (<>) { ... # code for each line } is equivalent to the following Perl-like pseudo code: unshift(@ARGV, ’-’) unless @ARGV; while ($ARGV = shift) { open(ARGV, $ARGV); while (<ARGV>) { ... # code for each line } }

      $! will contain the last filesystem error, which will make it a lot easier to see what's going wrong if you pass the wrong filename to open(), or don't have permissions etc. See also perlvar.

      The files are user-input, so the $file=<> has worked fine for me in the past.
      I think he was assuming that the file name was coming from the command line. @ARGV is the array of command line arguments, so "shift @ARGV" removes and returns the first argument. If you want the user to enter a file name, then it would be a good idea to prompt for it, e.g.:
      print "Enter filename: "; chomp(my $file = <STDIN>);
      what does the $! in the die function do?
      See perlvar. It is the error message for system/library calls. You want to check to see if open fails, and if it does, see why it failed.