943,809 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Marked Solved
  • Views: 864
  • Perl RSS
May 30th, 2009
0

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

Expand Post »
I have what I thought was a straightforward program.

Perl Syntax (Toggle Plain Text)
  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:
Perl Syntax (Toggle Plain Text)
  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.

Quote ...
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?
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,374 posts
since Jan 2008
May 30th, 2009
2

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

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:

Quote ...
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".

Perl Syntax (Toggle Plain Text)
  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:

Quote ...
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.
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
May 30th, 2009
0

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

Awesome. That solved it. Thank you very much.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,374 posts
since Jan 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: running multiple instances of a same perl-cgi script
Next Thread in Perl Forum Timeline: How do I post a string without user intervention ?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC