Perl syntax

Anthony agamemnon67-bJEeYj9oJeDQT0dZR+AlfA at public.gmane.org
Mon May 30 19:48:57 UTC 2005


When you say "mostly", I am hoping you are suggesting
it was my simplification. For that I apologize to any
other perlmongers in this forum.

As a matter of fact, I stayed away from global
variables, read-only's, cross data-type varibles,
positional parameters etc. for a reason, not
withstanding a whole discussion on block declarations,
module variable dependencies, sub-routines and
functions.

It was to state that simply putting "my" in a variable
declaration to get rid of an error message is a bad
idea, without understanding the scope. I just didn't
think everyone wanted a lecture on variables which
would inevitably take an email the size of a textbook

Hope that clarifies.

For those more interested in this:

http://to.pm.org/

I am sure there are many of us that subscribe to both
so I apologize for wasting tpm subscribers the
bandwidth.

Anthony



--- Andrew Hammond <ahammond-swQf4SbcV9C7WVzo/KQ3Mw at public.gmane.org> wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Anthony's post is mostly right. Declaration and
> scope of variables isn't
> a trivial topic in perl, but I'll take a swing at a
> simplified version.
> 
> use strict; requires that variables be declared
> _before_ they're used.
> 
> There are 3 ways to declare a variable:
> 
> 1) my declares (and creates) a "private" variable.
> This is a lexically
> scoped variable. It goes away when you leave the
> scope in which it was
> declared.
> 
> 2) our declares a "global" variable. It's doesn't go
> away until your
> program ends.
> 
> 3) local declares a dynamic scope of a variable.
> This should sound
> complicated because it is. It's used to "localize"
> variables.
> 
> If you want to really understand this you need to
> get a copy of the
> camel book and read chapter 4, to start with.
> 
> - --
> Andrew Hammond    416-673-4138   
> ahammond-swQf4SbcV9C7WVzo/KQ3Mw at public.gmane.org
> Database Administrator, Afilias Canada Corp.
> CB83 2838 4B67 D40F D086 3568 81FC E7E5 27AF 4A9A
> 
> 
> Anthony wrote:
> > Missed a line.
> > 
> > To declare a variable to only be valid from
> > declaration to the end of the block use.
> > 
> > local $var="data;
> > 
> > Anthony
> > --- Anthony <agamemnon67-bJEeYj9oJeDQT0dZR+AlfA at public.gmane.org> wrote:
> > 
> >>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
> >>
> > 
> > --
> > 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
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.0 (GNU/Linux)
> 
>
iD8DBQFCmzWUgfzn5SevSpoRAurFAKCrXtoYY9ECspcI7qV10qv9aQJlpQCfXJgs
> jhEW+5K97uT/HODFlAP69/Q=
> =DqXF
> -----END PGP SIGNATURE-----
> --
> 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
> 
=== message truncated ===

--
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