473,324 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Why use strict?

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
4 2204
RonB
589 Expert Mod 512MB
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
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
RonB
589 Expert Mod 512MB
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
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

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

Similar topics

9
by: Microsoft News | last post by:
I have a project that was created all with Option Strict OFF. Works great, not a problem with it. But if I turn Option Strict ON then I get a LOT of errors. My question, should I even care...
10
by: Mark McLellan | last post by:
Dear all Following the oft-repeated advice here and ciwas I have made my site nearly 4.01 strict (working on it). There are some items on which I would appreciate your advice: 1. Hidden...
13
by: Kieran | last post by:
I am designing a content management system and I want to make sure all pages entered into it's database by users are using valid HTML. I have designed the system to use HTML 4.01 Transitional...
1
by: Andy Crawford | last post by:
Even though I have set Strict Off, it is still on, thus causing this error. Here is the code in a templated Column of a DataGrid: (XPpro, Framework 1.1) <ItemTemplate> <TABLE width="100%">...
11
by: Daylor | last post by:
hi. im using option strict on. im doing in ,from the simple reason ,to be warn when there are implict conversion like string to int ,int to string. BUT. the price ,(now i see ), is very bad....
8
by: Rich | last post by:
Hello, If I leave Option Strict Off I can use the following syntax to read data from a Lotus Notes application (a NotesViewEntry object represents a row of data from a Lotus Notes View - like a...
17
by: David | last post by:
Hi all, I have the following problem: my program works fine, but when I add option strict at the top of the form, the following sub fails with an error that option strict does not allow late...
8
by: Rory Becker | last post by:
A wise man once said: "Never put off until runtime what you can fix at compile time." Actually I think he said it about 10 minutes before I started this post. I am a firm believer, like the...
8
by: =?Utf-8?B?R3JlZw==?= | last post by:
We have an application in our office that has the Option Strict option set to off right now. I do understand it should be set to ON, but right now, I'm just going to continue with it this way since...
92
by: Erwin Moller | last post by:
Hi group, I encoutered page validation error, but I don't know a way around. The page has the following doctype: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.