Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

How to correct code

by Anonymous Monk
on Sep 30, 2004 at 13:46 UTC ( [id://395341]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: How to correct code
by dragonchild (Archbishop) on Sep 30, 2004 at 14:25 UTC
    You threw in 'use strict' and tried it, now you're complaining. Sounds like you don't understand what 'use strict' is meant to do or why it complains about what it complains.

    I would strongly urge you to read a book on Perl, preferably one published by O'Reilly, that will give you a firm grounding in good programming practices, especially as they apply to Perl. Then, you will have a firmer grasp on what you would need to do to make your script run under 'use strict'.

    After that, try 'use warnings' and work through those issues, as well. Then, if you're brave, add taint-checking. :-)

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

      I would strongly urge you to read a book on Perl
      Good advice for new in perl...
Re: How to correct code
by Zaxo (Archbishop) on Sep 30, 2004 at 13:56 UTC

    You can say, use vars qw/@ll %those $variables/; at the top and your script will be just as it was.

    The better solution is to tighten up scopes and declare variables with my in the scopes where they are used.

    Btw, perl beats js like a red-headed stepson for text editing.

    After Compline,
    Zaxo

Re: How to correct code
by aquarium (Curate) on Sep 30, 2004 at 13:51 UTC
    this is a perl place...so don't ask for a javascript answer. when perl is in strict mode it requires that all variable declerations either have explicit package names added OR they be prefixed with "my" keyword. Hence, adding "my " in front of all the first instances of those variable names will fix it.

      You can't just add my to each first occurrence of each variable in a script and expect the script to work properly after that. What if the script has something like this?

      foreach $thingy ( @lots_of_thingies ) { $all_of_them .= $thingy; } print "$all_of_them\n";

      In that example, if $all_of_them is declared with my in the lexical scope bounded by the foreach loop's code block, you won't be able to print it after the loop runs out.

      strict enforces not just the use of variable declaraction, but it requires that additional thought be paid to design of lexical scopes. The OP would be wise to read perlintro, perlsub, perlsyn and my for starters. Sometimes a script is just beyond converting to compliance with strictures unless one is willing to take the time for a major rewrite.


      Dave

      could you show exact sample wherer should be added "my"?
        Here is a simple brief example, but like dragonchild says, to get the complete picture you should really get a Perl book. Personally I would recommend you borrow, steal or buy the "Camel" book from O'Reilly. It will fill you in on all the stuff necessary to get you going.
        use strict; #declare the variable $foo before using my $foo; $foo = 'hello world'; print $foo;
        my @lots_of_thingies; [...] my $all_of_them; foreach my $thingy ( @lots_of_thingies ) { $all_of_them .= $thingy; } print "$all_of_them\n";

        By side, this code cries for join...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://395341]
Approved by claree0
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-04-19 08:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found