On Tue, Sep 30, 2003 at 11:03:02AM +0100, Dillon, John wrote:
> According to
> http://vipe.technion.ac.il/~shlomif/lecture/Perl/Newbies/lecture2/argv.html
> the following program will do ...whatever (make a backup of files) and it
> takes the file specified at the command line.  I guessed from this that one
> has a .pl file with the following script, execute it at c: by typing its
> name and then the program stops and asks you what file you want to specify.
> But this does not happen.  So how do you get the filename into the script?
> 
> use strict;
> 
> my $filename = $ARGV[0];
> 
> open I, "<".$filename;
> open O, ">".$filename.".bak";
> print O join("",<I>);
> close(I);
> close(O);

You pass the file name as a command line argument

  > script.pl filename

I don't like the code, though.  All you really need is

  #!/usr/bin/perl -pi.bak

  # [ that's it, just the shebang ]

This also handles multiple files and does better error-
handling than the code you found.

If you want to write it yourself, you ought to:

  - copy the file line by line, instead of slurping it
  - check the success of open() and close()
  - consider using the three-argument form of open() 
  - consider using lexical filehandles
  - [style] prefer interpolation to concatenation

-- 
Steve

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

Reply via email to