How to make perl exit with an error code upon use of uninitialized-value?












0















I don't see how to make perl fail upon use of uninitialized-value. Is there a way to make this the default behavior? Thanks.



https://perlmaven.com/use-of-uninitialized-value










share|improve this question


















  • 2





    What have you tried so far? You didn't include any code, so it's impossible to know. Also, take a look at exit.

    – Matt Jacob
    Nov 22 '18 at 3:51











  • I don’t know what to try. I want the program to fail instead of just giving a warning. This must be done with some kind of configuration of Perl at the beginning of a script. Exit won’t work.

    – user1424739
    Nov 22 '18 at 4:04






  • 1





    Please edit your question and include the code you're claiming isn't working.

    – Matt Jacob
    Nov 22 '18 at 4:05






  • 6





    perldoc.perl.org/warnings.html#Fatal-Warnings

    – Shawn
    Nov 22 '18 at 4:14











  • The link from @Shawn seems to be what you want. But also see Carp::Always

    – zdim
    Nov 22 '18 at 6:10
















0















I don't see how to make perl fail upon use of uninitialized-value. Is there a way to make this the default behavior? Thanks.



https://perlmaven.com/use-of-uninitialized-value










share|improve this question


















  • 2





    What have you tried so far? You didn't include any code, so it's impossible to know. Also, take a look at exit.

    – Matt Jacob
    Nov 22 '18 at 3:51











  • I don’t know what to try. I want the program to fail instead of just giving a warning. This must be done with some kind of configuration of Perl at the beginning of a script. Exit won’t work.

    – user1424739
    Nov 22 '18 at 4:04






  • 1





    Please edit your question and include the code you're claiming isn't working.

    – Matt Jacob
    Nov 22 '18 at 4:05






  • 6





    perldoc.perl.org/warnings.html#Fatal-Warnings

    – Shawn
    Nov 22 '18 at 4:14











  • The link from @Shawn seems to be what you want. But also see Carp::Always

    – zdim
    Nov 22 '18 at 6:10














0












0








0








I don't see how to make perl fail upon use of uninitialized-value. Is there a way to make this the default behavior? Thanks.



https://perlmaven.com/use-of-uninitialized-value










share|improve this question














I don't see how to make perl fail upon use of uninitialized-value. Is there a way to make this the default behavior? Thanks.



https://perlmaven.com/use-of-uninitialized-value







perl






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 3:39









user1424739user1424739

1,26931731




1,26931731








  • 2





    What have you tried so far? You didn't include any code, so it's impossible to know. Also, take a look at exit.

    – Matt Jacob
    Nov 22 '18 at 3:51











  • I don’t know what to try. I want the program to fail instead of just giving a warning. This must be done with some kind of configuration of Perl at the beginning of a script. Exit won’t work.

    – user1424739
    Nov 22 '18 at 4:04






  • 1





    Please edit your question and include the code you're claiming isn't working.

    – Matt Jacob
    Nov 22 '18 at 4:05






  • 6





    perldoc.perl.org/warnings.html#Fatal-Warnings

    – Shawn
    Nov 22 '18 at 4:14











  • The link from @Shawn seems to be what you want. But also see Carp::Always

    – zdim
    Nov 22 '18 at 6:10














  • 2





    What have you tried so far? You didn't include any code, so it's impossible to know. Also, take a look at exit.

    – Matt Jacob
    Nov 22 '18 at 3:51











  • I don’t know what to try. I want the program to fail instead of just giving a warning. This must be done with some kind of configuration of Perl at the beginning of a script. Exit won’t work.

    – user1424739
    Nov 22 '18 at 4:04






  • 1





    Please edit your question and include the code you're claiming isn't working.

    – Matt Jacob
    Nov 22 '18 at 4:05






  • 6





    perldoc.perl.org/warnings.html#Fatal-Warnings

    – Shawn
    Nov 22 '18 at 4:14











  • The link from @Shawn seems to be what you want. But also see Carp::Always

    – zdim
    Nov 22 '18 at 6:10








2




2





What have you tried so far? You didn't include any code, so it's impossible to know. Also, take a look at exit.

– Matt Jacob
Nov 22 '18 at 3:51





What have you tried so far? You didn't include any code, so it's impossible to know. Also, take a look at exit.

– Matt Jacob
Nov 22 '18 at 3:51













I don’t know what to try. I want the program to fail instead of just giving a warning. This must be done with some kind of configuration of Perl at the beginning of a script. Exit won’t work.

– user1424739
Nov 22 '18 at 4:04





I don’t know what to try. I want the program to fail instead of just giving a warning. This must be done with some kind of configuration of Perl at the beginning of a script. Exit won’t work.

– user1424739
Nov 22 '18 at 4:04




1




1





Please edit your question and include the code you're claiming isn't working.

– Matt Jacob
Nov 22 '18 at 4:05





Please edit your question and include the code you're claiming isn't working.

– Matt Jacob
Nov 22 '18 at 4:05




6




6





perldoc.perl.org/warnings.html#Fatal-Warnings

– Shawn
Nov 22 '18 at 4:14





perldoc.perl.org/warnings.html#Fatal-Warnings

– Shawn
Nov 22 '18 at 4:14













The link from @Shawn seems to be what you want. But also see Carp::Always

– zdim
Nov 22 '18 at 6:10





The link from @Shawn seems to be what you want. But also see Carp::Always

– zdim
Nov 22 '18 at 6:10












2 Answers
2






active

oldest

votes


















2














Something like this perhaps:



#!/usr/bin/perl
use strict;
use warnings;
use warnings FATAL => qw[uninitialized];
use feature 'say';

my $foo;

say "Foo is $foo";
say "Don't get here";


Comment out the FATAL line to see the standard behaviour.






share|improve this answer































    0














    Dave's answer sure is the best pick for the described use case.



    Here is another solution, that demonstrates the use of the warning signal handler (see http://perldoc.perl.org/functions/warn.html).



    The benefit of using a signal handler is flexibility : you can trap any kind of warning, analyze it and then implement any behavior you like. In the given use case this is an overkill, it but can be useful in more complex cases.



    use strict;
    use warnings;
    use feature "say";

    local $SIG{__WARN__} = sub {
    if ($_[0] =~ /^Use of uninitialized value/) {
    die $_[0];
    } else {
    warn $_[0] ;
    }
    };

    my $foo;
    say "Foo is $foo";
    say "Dont get here";





    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423543%2fhow-to-make-perl-exit-with-an-error-code-upon-use-of-uninitialized-value%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      Something like this perhaps:



      #!/usr/bin/perl
      use strict;
      use warnings;
      use warnings FATAL => qw[uninitialized];
      use feature 'say';

      my $foo;

      say "Foo is $foo";
      say "Don't get here";


      Comment out the FATAL line to see the standard behaviour.






      share|improve this answer




























        2














        Something like this perhaps:



        #!/usr/bin/perl
        use strict;
        use warnings;
        use warnings FATAL => qw[uninitialized];
        use feature 'say';

        my $foo;

        say "Foo is $foo";
        say "Don't get here";


        Comment out the FATAL line to see the standard behaviour.






        share|improve this answer


























          2












          2








          2







          Something like this perhaps:



          #!/usr/bin/perl
          use strict;
          use warnings;
          use warnings FATAL => qw[uninitialized];
          use feature 'say';

          my $foo;

          say "Foo is $foo";
          say "Don't get here";


          Comment out the FATAL line to see the standard behaviour.






          share|improve this answer













          Something like this perhaps:



          #!/usr/bin/perl
          use strict;
          use warnings;
          use warnings FATAL => qw[uninitialized];
          use feature 'say';

          my $foo;

          say "Foo is $foo";
          say "Don't get here";


          Comment out the FATAL line to see the standard behaviour.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 9:34









          Dave CrossDave Cross

          48.5k34079




          48.5k34079

























              0














              Dave's answer sure is the best pick for the described use case.



              Here is another solution, that demonstrates the use of the warning signal handler (see http://perldoc.perl.org/functions/warn.html).



              The benefit of using a signal handler is flexibility : you can trap any kind of warning, analyze it and then implement any behavior you like. In the given use case this is an overkill, it but can be useful in more complex cases.



              use strict;
              use warnings;
              use feature "say";

              local $SIG{__WARN__} = sub {
              if ($_[0] =~ /^Use of uninitialized value/) {
              die $_[0];
              } else {
              warn $_[0] ;
              }
              };

              my $foo;
              say "Foo is $foo";
              say "Dont get here";





              share|improve this answer




























                0














                Dave's answer sure is the best pick for the described use case.



                Here is another solution, that demonstrates the use of the warning signal handler (see http://perldoc.perl.org/functions/warn.html).



                The benefit of using a signal handler is flexibility : you can trap any kind of warning, analyze it and then implement any behavior you like. In the given use case this is an overkill, it but can be useful in more complex cases.



                use strict;
                use warnings;
                use feature "say";

                local $SIG{__WARN__} = sub {
                if ($_[0] =~ /^Use of uninitialized value/) {
                die $_[0];
                } else {
                warn $_[0] ;
                }
                };

                my $foo;
                say "Foo is $foo";
                say "Dont get here";





                share|improve this answer


























                  0












                  0








                  0







                  Dave's answer sure is the best pick for the described use case.



                  Here is another solution, that demonstrates the use of the warning signal handler (see http://perldoc.perl.org/functions/warn.html).



                  The benefit of using a signal handler is flexibility : you can trap any kind of warning, analyze it and then implement any behavior you like. In the given use case this is an overkill, it but can be useful in more complex cases.



                  use strict;
                  use warnings;
                  use feature "say";

                  local $SIG{__WARN__} = sub {
                  if ($_[0] =~ /^Use of uninitialized value/) {
                  die $_[0];
                  } else {
                  warn $_[0] ;
                  }
                  };

                  my $foo;
                  say "Foo is $foo";
                  say "Dont get here";





                  share|improve this answer













                  Dave's answer sure is the best pick for the described use case.



                  Here is another solution, that demonstrates the use of the warning signal handler (see http://perldoc.perl.org/functions/warn.html).



                  The benefit of using a signal handler is flexibility : you can trap any kind of warning, analyze it and then implement any behavior you like. In the given use case this is an overkill, it but can be useful in more complex cases.



                  use strict;
                  use warnings;
                  use feature "say";

                  local $SIG{__WARN__} = sub {
                  if ($_[0] =~ /^Use of uninitialized value/) {
                  die $_[0];
                  } else {
                  warn $_[0] ;
                  }
                  };

                  my $foo;
                  say "Foo is $foo";
                  say "Dont get here";






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 '18 at 20:57









                  GMBGMB

                  20.6k41028




                  20.6k41028






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423543%2fhow-to-make-perl-exit-with-an-error-code-upon-use-of-uninitialized-value%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      How to change which sound is reproduced for terminal bell?

                      Can I use Tabulator js library in my java Spring + Thymeleaf project?

                      Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents