Re: Can't use string as a symbol ref
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/
Re: Can't use string as a symbol ref
Maybe tell us what you are trying to do? Try this: > #!/usr/bin/perl -w > use strict; > my $input = $ARGV[0]; # File handle my $FH; # Check the file is a normal file and exists die "File does not exist\n" if not -f $input; # Open the file handle for read only, file named by $input # Or die and print $! which is probably not the error message # variable. perlvar would tell you the corrent variable. open $FH, '<', $input or die "$!"; # Read from the file handle my @devices = <$FH> ; # The the array elements with a ", " > print "devices = ", join (", ", @devices), " \n"; > > __END__ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Can't use string as a symbol ref
On 12/17/07, ciwei2103 <[EMAIL PROTECTED]> wrote: > Can somebody enlighten me what I'm doing wrong? > my $input = $ARGV[0]; > > my @devices = <$input> ; $input is a string, since it comes from @ARGV; but you're using it as if it's a filehandle. Do you need open()? Hope this helps! --Tom Phoenix Stonehenge Perl Training -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Can't use string as a symbol ref
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. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/