Getting error using "strict" mode due to variable name?

Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jan 2008
Posts: 3,828
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Getting error using "strict" mode due to variable name?

 
0
  #1
May 30th, 2009
I have what I thought was a straightforward program.

  1. #!/usr/bin/perl -w
  2. use strict;
  3.  
  4. $a = 6;
  5. $b = 9;
  6. $c = 7;
  7.  
  8. print $a . "\n";
  9. print $b . "\n";
  10. print $c . "\n";

I assumed this would display:
  1. 6
  2. 9
  3. 7

and it does when I remove use strict; on line 2. When I leave it in the program, I get this error.

Global symbol "$c" requires explicit package name at C:\Users\Rick\Documents\PerlStuff\perlexperiment.pl line 6.
Global symbol "$c" requires explicit package name at C:\Users\Rick\Documents\PerlStuff\perlexperiment.pl line 10.
Execution of C:\Users\Rick\Documents\PerlStuff\perlexperiment.pl aborted due to compilation errors.
It gives me this error for c , but not a or b . a , b , and c are just variable names here, right?
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Getting error using "strict" mode due to variable name?

 
2
  #2
May 30th, 2009
The perl documentation would have explained all of your errors. But I'll short-cut it for you.

First, $a and $b are variables used by perl for sorting and should not be used by you except for that purpose. Since they are global package variables you don't have to declare them with "my".

Quoted from the sort functions documentation:

In the interests of efficiency the normal calling code for subroutines is bypassed, with the following effects: the subroutine may not be a recursive subroutine, and the two elements to be compared are passed into the subroutine not via @_ but as the package global variables $a and $b (see example below). They are passed by reference, so don't modify $a and $b . And don't try to declare them as lexicals either.
So $a and $b need to be changed and all your variable need to be declared within scope using "my".

  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4.  
  5. my $x = 6;
  6. my $y = 9;
  7. my $c = 7;
  8.  
  9. print $x, "\n";
  10. print $y, "\n";
  11. print $c, "\n";

I changed "." to "," in the print lines because using the dot (concatenation) is slow. 'print' is a list operator so it expects a list of things to print, so using a comma to create a list of thing to print is very fast as it doesn't force perl to build any intermitent strings first.

Quoted from the print operators documentation:

Prints a string or a comma-separated list of strings.
Also note the use of "use warnings" in place of the -w switch. All modern versions of perl come with the warnings pragma and it should be used in place of the -w switch. You have more control over how it behaves and where it is used than the -w switch, which is an all or nothing proposition. See the warnings pragma documentation for more details.
Last edited by KevinADC; May 30th, 2009 at 7:57 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,828
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Getting error using "strict" mode due to variable name?

 
0
  #3
May 30th, 2009
Awesome. That solved it. Thank you very much.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC