my creates dynamic allocation for the variable within the current scope only. So for example, if you have a bunch of subroutines all having my %parm = @_ at the beginning, then after calling each other, even recursively, the values in %parm remain distinct and preserved across all such calls and returns.

local, as has been said, makes special use of a global or package variable. Whereas it may be unusual to use it for ordinary variables, given that my is sufficient for this purpose, it comes into its own for built-in variables. For example, if a subroutine uses $_, it can ensure preservation of the callers value for $_, simply by declaring local $_, whereas my $_ just won't be allowed, even without the strict or warning pragmas. So local is absolutely necessary for this and is almost a sine qua non if one wants to use $_ in a module method without "hurting" any possible caller.

The argument about goto was best explained by Kathleen Jensen in the 1970s in the definitive Pascal book known to most as "Jensen and Wirth". The arguments therein apply to Perl as much as any "block structured" language, that is to say, whereas block structure might technically obviate the need for goto, it remains justified and even encouraged (by me at least) for the special case of breaking out of structure on encountering an exception. For example:-

sub fred { my $self = shift; $self -> parse or goto SORRYFRED; while ( $self -> codegen ) { $self -> codeprint or goto SORRYFRED; } return 1; SORRYFRED: print STDERR $self -> formatmsg; $self -> { SEVERITY } < 2 or $self -> cexit; # case of fatal 0; # case of warning }
If an exception occurs in a while loop, or worse a nested while loop, why punish yourself and your code by messing with the loop conditions and artificially adding huge if blocks and so on? It must be more readable and maintainable to jump out to an error handler at a higher scope like in above sketched example.

Finally, if you don't want to return explicitly, the value of the statement of the routine will get returned to the caller anyway, so in the above example, there are two explicit returns (update: actually I mean an explicit return and a call to an exit routine to manage exit codes for example) and the final way out at the last line is just a 0; to return 0 to the caller.

One world, one people


In reply to Re: Several stupid questions about subs by anonymized user 468275
in thread Several stupid questions about subs by bryan172

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.