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


In reply to Re: best way to learn use strict by Zaxo
in thread best way to learn use strict by Grygonos

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.