Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: exit this way

by AnomalousMonk (Archbishop)
on Aug 31, 2015 at 22:34 UTC ( [id://1140577]=note: print w/replies, xml ) Need Help??


in reply to exit this way

Further to Your Mother's reply: Because Perl has no  main() function and allows intermixing function calls and definitions, strange things can happen. Better by far, IMHO, to have someone put in a function call and scratch their head (even for quite a while) about why it is never executed than to have the code compile and apparently run correctly for hours/days/weeks/... before realizing that, hey, something ain't right! Here's an (admittedly rather contrived) example of goofy behavior:

c:\@Work\Perl\monks>perl -wMstrict -le "S(3); T(); S(9); T(); ;; exit_here_to_avoid_weirdness(); ;; my $x = 42; sub S { $x = $_[0]; printf qq{in S: x == $x }; T(); } ;; sub T { ++$x; print qq{in T: x == $x}; } ;; sub exit_here_to_avoid_weirdness { ;;; } T(); " in S: x == 3 in T: x == 4 in T: x == 5 in S: x == 9 in T: x == 10 in T: x == 11 in T: x == 43
Is that 43 result from the last call to  T() correct? Maybe better not to make it at all:
c:\@Work\Perl\monks>perl -wMstrict -le "S(3); T(); S(9); T(); ;; exit_here_to_avoid_weirdness(); ;; my $x = 42; sub S { $x = $_[0]; printf qq{in S: x == $x }; T(); } ;; sub T { ++$x; print qq{in T: x == $x}; } ;; sub exit_here_to_avoid_weirdness { exit; } T(); " in S: x == 3 in T: x == 4 in T: x == 5 in S: x == 9 in T: x == 10 in T: x == 11
(Of course, it would be nice to have a "code not reachable" message, but that's life.)


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: exit this way
by 1nickt (Canon) on Sep 01, 2015 at 02:32 UTC

    it would be nice to have a "code not reachable" message

    There isn't such a check for post-exit(), but Perl::Critic definitely has a rule for pre-use strict;. I guess one could write a rule that ignored subroutines...

    I guess there is!

    The way forward always starts with a minimal test.

        Oof. Updated.

        I ran a little test script but my .perlcriticrc had got borked somehow so perlcritic ran with -5 (why is that the default, anyway?), and the rule has a severity of 4. I didn't notice anything wrong because "Code before strictures enabled" has a severity of 5, so it did throw an error. Thanks for the correction. (When I first went to answer the question I thought I remembered there was a rule!)

        #!/usr/bin/perl use warnings; my $foo = 'bar'; use strict; exit(0); my $baz = 'quux';
        $ perlcritic 1140661.pl Code before strictures are enabled at line 3, column 1. See page 429 +of PBP. (Severity: 5)
        $ perlcritic -4 1140661.pl Code before strictures are enabled at line 3, column 1. See page 429 +of PBP. (Severity: 5) Unreachable code at line 9, column 1. Consider removing it. (Severit +y: 4) $

        The way forward always starts with a minimal test.
Re^2: exit this way
by ExReg (Priest) on Sep 01, 2015 at 04:50 UTC
    This will probably become a standards issue, and I will probably start putting the exits in. I hate it when I have to abide by standards that I get after others about. I know I would never leave stray bits of code roaming around after the subs start. ;^)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-26 00:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found