I assume you want to learn how to write code that strict.pm likes. A worthy goal.
Try combining use strict; with use diagnostics;. That will give you verbose informative error messages when strict kicks up a fuss,
$ perl -Mstrict -Mdiagnostics -e'$foo = "bar";$bar = "baz";print $$foo
+, $/'
Global symbol "$foo" requires explicit package name at -e line 1
Global symbol "$bar" requires explicit package name at -e line 1
Global symbol "$foo" requires explicit package name at -e line 1
Execution of -e aborted due to compilation errors (#1)
(F) You've said "use strict vars", which indicates
that all variables must either be lexically scoped
(using "my"), declared beforehand using "our", or
explicitly qualified to say which package the global
variable is in (using "::").
Uncaught exception from user code:
Global symbol "$foo" requires explicit package name at -e line 1
Global symbol "$bar" requires explicit package name at -e line 1
Global symbol "$foo" requires explicit package name at -e line 1
Execution of -e aborted due to compilation errors
$
Ok, that's clear enough, stick in a my before the first use of $foo and $bar,
$ perl -Mstrict -Mdiagnostics -e'my $foo = "bar";my $bar = "baz";print
+ $$foo, $/'
Can't use string ("bar") as a SCALAR ref while "strict refs" in use at
+ -e line
1 (#1)
(F) Only hard references are allowed by "strict refs". Symbolic
references are disallowed. See perlref'
Uncaught exception from user code:
Can't use string ("bar") as a SCALAR ref while "strict refs" i
+n use at -e line 1
$
Now that's a little meatier. It addresses a design weakness of the program. To clear that stricture we need to think what we mean to be doing. Is it more important to have a string representation of $bar's name, or is the reference the important thing? In the former case, a hash is preferred for holding the data, so we can say $hash{$foo}, where before we said $$foo. If the reference is the thing, we make it a hard reference. Let's assume that the reference is the thing,
$ perl -Mstrict -Mdiagnostics -e'my $foo = \my $bar; $bar = "baz"; pri
+nt $$foo, $/'
baz
$
Success. It doesn't take many sessions of this to teach you the rules. Have fun!
After Compline, Zaxo |