Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Error while using use srict

Posted by kitwalker2886 (kitwalker2886), 9 August 2008
Hi every body,
          I am a pretty lazy programmer and after figuring out the basics of perl, i never got around to picking up good programming practices. One of the effects of this is that i never used 'use strict' in my codes..
          Anyways when i decided to start doing it and did so. But i got a problem in a code that i was trying out because of 'use strict'.
        My code looks something like this..

use strict;
sub domainlist
   while  (...)
      my @domains;

When i tried running this, i got an error message saying "global symbol @domains requires explicit package name"

I tried fiddiling around with the code and found that putting the declaration of @domains outside the while loop solves the problem..
     The code works now. But can anyone tell me why i had this problem? Does having an array declaration inside a while loop cause problems? I am also using another scalar variable that is declared inside the while loop,  but no problems with that.. why?

thanks in advance,

Posted by KevinAD (KevinAD), 9 August 2008
Its a scoping problem. When you use "strict" and declare variables with "my" within a block of code, meaning whats between the curly brackets, the variable is only "visible" within that block of code:

while {
 this is the block

That is the scope of the variable. It is very common, and often necessay, to declare some variables outside a block of code that you will do something with inside a block of code but need access to the variable outside the block of code that does something with the variable. For example:

my @array = ();
foreach my $key (keys %hash){
  push @array, $hash{$key};
print "$_\n" for @array;

If you never used strict and were always using global variables you probably did not pass variables into and out of subroutines either. That is something that becomes necessay when using strict. A very simple example:

my $foo = 1;
$foo = add_one($foo);
sub add_one {
  my $foo = $_[0];

$foo inside and $foo outside the block are not the same variable.

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho