By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,661 Members | 1,330 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,661 IT Pros & Developers. It's quick & easy.

Why use strict?

P: 6
This is my 2nd day as a Perl on Windows programmer (5.12 Strawberry) and i heard it many times to use strict and warnings, which seems like good advice. And use strict & use warnings has helped heaps.

But in one instance my code works only without use strict and i cannot find the reason for the error that i get in all my googling so far... Are there people with huge brains and eons of experience who can show me why this bit of code won't pass strict?

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my $addone = 0;
  7. my $argnum = 0;
  8. my $numArgs = 0;
  9.  
  10. my @FILE = (0..0);
  11. my @fileName = (0..0);
  12. my @fileSize = (0..0);
  13.  
  14. ## With Perl, command-line arguments are stored in the array named @ARGV.  $ARGV[0] contains the first argument, $ARGV[1] contains the second argument, etc.  $#ARGV is the subscript of the last element of the @ARGV array, so the number of arguments on the command line is $#ARGV + 1.  
  15. $numArgs = $#ARGV + 1;
  16. foreach $argnum (0 .. $#ARGV) 
  17. {
  18.     $addone = $argnum + 1;
  19.     $fileName[$addone] = $ARGV[$argnum];
  20.    # Create File Handle.
  21.     $FILE[$addone] = $fileName[$addone];
  22.    # Check the file is a normal file and exists.
  23.     die "$FILE[$addone] does not exist\n" if not -f $FILE[$addone];
  24.    # Open file.
  25.     open $FILE[$addone], "<", $fileName[$addone] or die "$FILE[$addone] cannot be opened!";
  26.     binmode ($FILE[$addone]);
  27.    # Get file sizes.
  28.     $fileSize[$addone] = -s $fileName[$addone];
  29.     print ("File $argnum \(Size:$fileSize[$addone]\):$fileName[$addone]\n");
  30. }
  31.  
Error: Can't use string ("foo bar"...) as a symbol ref while "strict refs" in use
at M:\M\foobar+-.pl line 25.
Jun 22 '10 #1
Share this Question
Share on Google+
4 Replies


Expert Mod 100+
P: 589
Line 21 is your main problem. There are several other issues, but that's the one causing your error.

Lexical filehandles need to be declared butt not defined with a value prior to the open call. Comment out or remove lines 21 and 23.
Jun 22 '10 #2

P: 6
Thanks Ron, i changed the code as you said and it passes strict, now all i have to do is study what you said and learn what's going on there.

These are the code changes (i'm sure i'm still learning here, but few tutorials talk about how to implement file handle arrays):

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my $addone = 0;
  7. my $argnum = 0;
  8. my $numArgs = 0;
  9.  
  10. my @fileName = (0..0);
  11. my @fileSize = (0..0);
  12. my @FILEHANDLE = (0..0);
  13.  
  14. $numArgs = $#ARGV + 1;
  15.  
  16. foreach $argnum (0 .. $#ARGV) 
  17. {
  18.     $addone = $argnum + 1;
  19.     $fileName[$addone] = $ARGV[$argnum];
  20.    # Open file.
  21.     open ($FILEHANDLE[$addone], "<./$fileName[$addone]\0") or die "$fileName[$addone] cannot be opened for reading: $!";
  22.     binmode ($FILEHANDLE[$addone]);
  23.    # Get file sizes.
  24.     $fileSize[$addone] = -s $fileName[$addone];
  25.     print ("File $argnum \(Size:$fileSize[$addone]\):$fileName[$addone]\n");
  26. }
  27.  
Jun 22 '10 #3

Expert Mod 100+
P: 589
Using an array for filehandles is not really needed but it can in some cases make things easier, however it can also make it messy/convoluted if you're not careful. Personally, I prefer to use a hash instead of an array.

Is this for a class assignment?
Jun 22 '10 #4

P: 6
Hi RonB, I think i'm finding out what these filehandles are, they're 'special'. I don't think they come in arrays like other variables.

I wanted to just increment everything in a simple loop, so i thought an array[i], and the number of args will vary each run.

I did not want to use the same filehandle over and over and mess-up/clobber something, so i wanted filehandles that i could increment along with everything else in the loop. The filehandles need to stay open for the whole program.

It's not a school assignment, but it's as simple and puzzling as one, lol. I'm just at the 2nd day learning Perl. =)
Jun 22 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.