Edward Wijaya wrote: > Hi,
Hello,
> Why my code below fail to open and
> print the file contents
>
> when I do:
>
> perl mycode.pl -f filename
>
>
> __BEGIN__
> use strict;
> use warnings;
>
> use Getopt::Std;
> use vars qw($f);
> getopts('f:');getopts( 'f:' ) creates the variable $opt_f and stores the following argument 'filename' in that variable and *REMOVES* those arguments from @ARGV so that @ARGV is now empty.
> my $f = $ARGV[0];
Since @ARGV is now empty, $f is also empty (undef).
> open ( INFILE, '<', $f) > or die "$0 : failed to open input file $f : $!\n"; > close ( INFILE ); > > while ( <> )
Since @ARGV is now empty there is nothing for <> to open and read from.
> { > print $_; > } __END__
You should do it like this instead:
use strict; use warnings;
use Getopt::Std;
getopts( 'f:', \my %opt );
open INFILE, '<', $opt{ f } or die "$0 : failed to open input file $opt{f} : $!";while ( <INFILE> ) {
print;
}close INFILE;
__END__
John -- use Perl; program fulfillment
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>
