Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-20 03:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found