On Dec 17, 2007 10:19 AM, ciwei2103 <[EMAIL PROTECTED]> wrote:
> Can somebody enlighten me what I'm doing wrong?
>
> I have a list in a file , "test.dat"
>
> sh> cat test1.dat
> 0039
> 0038
>
> sh>  cat test1.pl
> #!/usr/bin/perl -w
> use strict;
> my $input = $ARGV[0];
>
> my @devices =  <$input>  ;
> print "devices =  @devices \n";
>
> __END__
>
> now run it with
> sh> test.pl test1.dat
>
> Can't use string ("test1.dat") as a symbol ref while "strict refs" in
> use at ./test.pl line xx.

In addition to using open as others have suggested, if the only
arguments the script is expecting are files to process, you don't need
to use open; the <> operator will automagically open files named in
@ARGV if given no file handle:

#!/usr/bin/perl

use strict;
use warnings;

my @devices = <>;
print "devices = @devices\n";

However, it is considered a bad idea to slurp all of a file into an
array (unless you are one hundred percent certain the files are
small).  It is better to write your code to handle things in pieces (a
line at a time).  A common way to do this is to use a while loop:

#!/usr/bin/perl

use strict;
use warnings;

while (my $device = <>) {
    #do something with each device
}

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to