hakkr has asked for the wisdom of the Perl Monks concerning the following question:
Does pre declaring variables before you use them have any memory/performance benefit for the interpreter by providing type information. I know in other strongly typed languages you should always declare the type of your variable but does it matter in perl or am I doing it by prepending @ or % or $.my @foo; my %bar; my $yuk; and my @foo=(); my %bar=(); my $yuk={};
Also is putting a 'my' call inside a loop to clear the variable advisable?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Predeclaration
by broquaint (Abbot) on Jul 16, 2002 at 11:04 UTC | |
Does pre declaring variables before you use them have any memory/performance benefit for the interpreter by providing type information.Essentially no. There are several reasons for this one of which is that perl isn't a strongly typed language so the compiler isn't designed to take advantage of 'type hints'. In fact it is every so slightly quicker to *not* predeclare your variables in terms of execution speed as it increases the amount of operations to be performed So as you can see the sub *without* the anonymous hash assignment was ever so slightly faster, but I'd say the difference is fairly negligible. I know in other strongly typed languages you should always declare the type of your variable but does it matter in perl or am I doing it by prepending @ or % or $.There are only really two types in perl and they are scalars and lists and everything else from there on out is an abstraction. For more information on strongly-typed languages and perl see. Ovid's Griping about Typing, and for info about data types in perl see the perldata manpage. Also is putting a 'my' call inside a loop to clear the variable advisable?I think you may have a misunderstanding of what function my() performs. Declaring a variable with my() will create a new variable in the current lexical scope, that's all. So you're not 'clearing' the variable you're just creating a new variable in the current lexical scope e.g
HTH
_________ | [reply] [d/l] [select] |
by hakkr (Chaplain) on Jul 16, 2002 at 11:27 UTC | |
I suppose it must just be for improved readability and to help the stongly typed converts | [reply] |
by Anonymous Monk on Jul 16, 2002 at 13:13 UTC | |
People generally predeclare with my to assist with error-checking. If you add the pragma use strict; to the top of your program, all variables have to be declared with my or our before they're used. This helps eliminate errors due to variable name typos. For example:
will produce an error. | [reply] [d/l] [select] |
|
Re: Predeclaration
by Abigail-II (Bishop) on Jul 16, 2002 at 12:42 UTC | |
and Both will declare the variables at compile time, and assign an empty list to them at run time. Using Devel::Peek shows there is no difference.
@foo_1:
SV = RV(0x81584b4) at 0x814490c
REFCNT = 1
FLAGS = (TEMP,ROK)
RV = 0x814ead4
SV = PVAV(0x8145e5c) at 0x814ead4
REFCNT = 2
FLAGS = (PADBUSY,PADMY)
IV = 0
NV = 0
ARRAY = 0x0
FILL = -1
MAX = -1
ARYLEN = 0x0
FLAGS = (REAL)
@foo_2:
SV = RV(0x81584b4) at 0x814490c
REFCNT = 1
FLAGS = (TEMP,ROK)
RV = 0x814eaec
SV = PVAV(0x8145e28) at 0x814eaec
REFCNT = 2
FLAGS = (PADBUSY,PADMY)
IV = 0
NV = 0
ARRAY = 0x0
FILL = -1
MAX = -1
ARYLEN = 0x0
FLAGS = (REAL)
%bar_1:
SV = RV(0x81584b4) at 0x814490c
REFCNT = 1
FLAGS = (TEMP,ROK)
RV = 0x814eb10
SV = PVHV(0x81535b8) at 0x814eb10
REFCNT = 2
FLAGS = (PADBUSY,PADMY,SHAREKEYS)
IV = 0
NV = 0
ARRAY = 0x0
KEYS = 0
FILL = 0
MAX = 7
RITER = -1
EITER = 0x0
%bar_2:
SV = RV(0x81584b4) at 0x814490c
REFCNT = 1
FLAGS = (TEMP,ROK)
RV = 0x814eba0
SV = PVHV(0x81535f0) at 0x814eba0
REFCNT = 2
FLAGS = (PADBUSY,PADMY,SHAREKEYS)
IV = 0
NV = 0
ARRAY = 0x0
KEYS = 0
FILL = 0
MAX = 7
RITER = -1
EITER = 0x0
However, there is a big difference between
and The former assigns an undefined value to $yuk, while the latter assigns a reference to an anonymous hash to $yuk. Devel::Peek will show the difference:
$yuk_1:
SV = NULL(0x0) at 0x814eacc
REFCNT = 1
FLAGS = (PADBUSY,PADMY)
$yuk_2:
SV = RV(0x8158458) at 0x814eae4
REFCNT = 1
FLAGS = (PADBUSY,PADMY,ROK)
RV = 0x814490c
SV = PVHV(0x8153550) at 0x814490c
REFCNT = 1
FLAGS = (SHAREKEYS)
IV = 0
NV = 0
ARRAY = 0x0
KEYS = 0
FILL = 0
MAX = 7
RITER = -1
EITER = 0x0
Abigail | [reply] [d/l] [select] |
|
Re: Predeclaration
by jmcnamara (Monsignor) on Jul 16, 2002 at 11:31 UTC | |
The my() declares the scope to the varaible and not the type. The type doesn't change.* In your example the variables will remain an array, a hash and a scalar no matter what you put into them. Perhaps what you are really asking is whether there is any advantage of initialising variables like this. As far as I know there is no substantive difference between the two of these: However, you can gain some performance increase by pre-extending an array or a hash if you know what size it is likely to be:
--
| [reply] [d/l] [select] |
|
Re: Predeclaration
by flounder99 (Friar) on Jul 16, 2002 at 14:43 UTC | |
Is anyone else surprised by the results? -- flounder | [reply] [d/l] [select] |
by Abigail-II (Bishop) on Jul 16, 2002 at 14:49 UTC | |
Is anyone else surprised by the results?People who read the manual pages will not be surprised. From the manual page about defined:
Use of "defined" on aggregates (hashes and arrays)
is deprecated. It used to report whether memory
for that aggregate has ever been allocated. This
behavior may disappear in future versions of Perl.
You should instead use a simple test for size:
if (@an_array) { print "has array elements\n" }
if (%a_hash) { print "has hash members\n" }
Abigail | [reply] [d/l] |
by flounder99 (Friar) on Jul 16, 2002 at 17:39 UTC | |
-- flounder ps. For those of you outside the USA (or who never turned on a TV after 11pm in the US) Miss Cleo was a (so called) psychic with a bad fake Jamacan accent who pushed a psychic hotline. The actress who played her recently appeared in court and admitted she was not Jamacan during a fraud investigation. | [reply] |
|
Re: Predeclaration
by cybear (Monk) on Jul 16, 2002 at 13:46 UTC | |
Background: If you call the value of a segment of memory,
Abigail says that the variable is assigned an empty list
If that is true then there should be no security differences
| [reply] [d/l] [select] |
by Abigail-II (Bishop) on Jul 16, 2002 at 13:59 UTC | |
Abigail | [reply] |
by IlyaM (Parson) on Jul 16, 2002 at 15:11 UTC | |
It is impossible with pure Perl and no XS modules. But Devel::Pointer can turn Perl code into C pointers nightmare :)
-- | [reply] |