Perl syntax
Anthony
agamemnon67-bJEeYj9oJeDQT0dZR+AlfA at public.gmane.org
Mon May 30 14:18:56 UTC 2005
It is very important that you understand exactly what
it is you are doing to fix the errors. Someone said
just put
my $var="data";
True it does stop the error messages but unless you
understand the correct scoping in the variables, your
program will have a lot worse logic problems down the
road.
You have to ensure your variables are scoped properly.
For example, when you:
use strict;
You must ensure to declare whether your variables are
dynmaically or lexically scoped.
Therefore you cannot declare a variable like this:
$var="data";
You have to tell the Perl interpreter in which blocks
the variables are valid.
my $var="data";
in the main prgram will ensure that $var is always
valid in the program. If it is placed inside its' own
block, $var will be valid inside that block, for
instance a subroutine. This allows you to reuse
variables. so $var can be different inside different
blocks.
Additional you can set a variable to only be valid
from the declaration until the end of the block.
Hope that helps.
Anthony
--- John Wildberger <wildberger-iRg7kjdsKiH3fQ9qLvQP4Q at public.gmane.org> wrote:
> In the recent thread on this subject the 'use
> strict' pragma was very strongly
> suggessted.
> Here is a very simple program urc2_1.pl:
>
> #! /usr/bin/perl -w
> use strict;
> while (<>){
> $count{$ARGV}++;
> }
> foreach $file (sort keys %count){
> print "$file has $count{$file} lines\n";
> }
>
> When executed it gives the following error messages:
>
> [john]: 09:46 AM [~/perl]
> $ ./urc2_1.pl urc2_1.pl
> Global symbol "%count" requires explicit package
> name at ./urc2_1.pl line 4.
> Global symbol "$file" requires explicit package name
> at ./urc2_1.pl line 6.
> Global symbol "%count" requires explicit package
> name at ./urc2_1.pl line 6.
> Global symbol "$file" requires explicit package name
> at ./urc2_1.pl line 7.
> Global symbol "%count" requires explicit package
> name at ./urc2_1.pl line 7.
> Global symbol "$file" requires explicit package name
> at ./urc2_1.pl line 7.
> Execution of ./urc2_1.pl aborted due to compilation
> errors.
>
> Without the 'use strict' pragma it executes without
> errors like this:
>
> [john]: 09:44 AM [~/perl]
> $ ./urc2_1.pl urc2_1.pl
> urc2_1.pl has 9 lines
>
> What changes to the program will I have to make to
> correct this?
> John
> --
> The Toronto Linux Users Group. Meetings:
> http://tlug.ss.org
> TLUG requests: Linux topics, No HTML, wrap text
> below 80 columns
> How to UNSUBSCRIBE:
> http://tlug.ss.org/subscribe.shtml
>
--
The Toronto Linux Users Group. Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml
More information about the Legacy
mailing list