Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Strictly nested sub warnings

by GrandFather (Saint)
on Oct 05, 2010 at 22:37 UTC ( [id://863697]=note: print w/replies, xml ) Need Help??


in reply to Strictly nested sub warnings

Unrelated (directly) to your question, but in keeping with your desire to update your coding practises:

  1. Don't use subroutine prototypes. They probably don't do what you think.
  2. If you don't use prototypes you don't need to forward declare subs.
  3. Don't call subs using &subName. It probably doesn't do what you think either.
  4. Don't nest named subs, but you know that now.
  5. Generally use string interpolation instead of concatenation. For example: print "Found $params{numoLinks} Symbolic Links...\n";
  6. Initialise variables when you declare them. For example: my %params = (recurseDir => '.');
True laziness is hard work

Replies are listed 'Best First'.
Re^2: Strictly nested sub warnings
by pobocks (Chaplain) on Oct 06, 2010 at 04:09 UTC

    Out of curiousity, why #5? I don't question that it's the case (you know better than me by job lots), I just wondered what the reason was.

    for(split(" ","tsuJ rehtonA lreP rekcaH")){print reverse . " "}print "\b.\n";

      It's fairly minor, but using concatenation when you could use interpolation instead tends to obscure the result you are aiming for. Compare the following two print statements:

      use strict; use warnings; my $tsuJ = 'tsuJ'; my $rehtonA = 'rehtonA'; my $lreP = 'lreP'; my $rekcaH = "\nrekcaH"; print join ' ', map {scalar reverse} split / /, $tsuJ . ' ' . $rehtonA + . ' ' . $lreP . ' ' . $rekcaH; print join ' ', map {scalar reverse} split / /, "$tsuJ $rehtonA $lreP +$rekcaH";
      True laziness is hard work
      Especially if you're joining lots of vars with short strings, concatenation can leave you drowning in symbols.
        For short variables I agree 100%.

        For more complex variables, I tend to go back to concatenation to make sure I get what I want.

        print "Guess what you get: $hashref->{$lookup1[$i]}->{$lookup2[$i]}\n" +;

        Also, with syntax highlighting, punctuation actually makes the code more readable than interpolation IMO.

        -- Time flies when you don't know what you're doing
Re^2: Strictly nested sub warnings
by raybies (Chaplain) on Oct 06, 2010 at 11:57 UTC

    It's probably a stupid question, but why not use prototypes?

    I like that I get some argument checking (not a whole lot, but that's actually better most of the time) by declaring named subs (i.e.  sub mySubroutine ($$) has two expected scalar args) with args, but I also like to stuff my subs at the bottom of my file, and so perl issues a warning if I don't put the prototype at the beginning of the code.

    Are you suggesting that to become more Perly, one must abandon naming arguments altogether in subs?

    Thanks again, all, for the suggestions. --Ray

        Thanks! I was just reading that exact node in the Meditations section... --Ray

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-03-29 10:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found