Nicpetbio23! has asked for the wisdom of the Perl Monks concerning the following question:

I need a code to tell me when an undefined scalar is being printed out. I am new at this and don't know where to start.

Replies are listed 'Best First'.
Re: Undefined scalar
by stevieb (Canon) on Jul 11, 2017 at 16:02 UTC

    All you have to do is put the following at the top of your script:

    use warnings;

    Add the following as well to report on all problems with your code:

    use strict;

    Warning example for undefined variable:

    use warnings; use strict; my $x; print "$x\n";

    Output:

    Use of uninitialized value $x in concatenation (.) or string at script +.pl line 4.

    strict error for undeclared variable:

    use warnings; use strict; print "$y\n";

    Output:

    Global symbol "$y" requires explicit package name (did you forget to d +eclare "my $y"?) at script.pl line 4.
Re: Undefined scalar
by Eily (Monsignor) on Jul 11, 2017 at 16:49 UTC

    If it's for debugging, another way to tell the difference between the empty string and an undef value is to use Data::Dumper or Data::Dump. The first one requires no installation, but one of the reasons I like the second one better is because it displays non-printable character explicitly with escape sequences by default (it's the Useqq option in Data::Dumper), so it helps tell the difference between undef, "", "\n" or even "\0". As far as I'm concerned, whenever it's for debugging I don't print it I dump it.

    Edit: eg:

    use Data::Dump qw( pp ); pp undef; pp ""; pp "\n"; pp "\0";

    NB: of course those two modules are also useful for displaying complex data structures.

Re: Undefined scalar
by Discipulus (Canon) on Jul 11, 2017 at 16:52 UTC
    Hello Nicpetbio23!,

    more ways are possible. You might profit the ternary operator described in perlop too: IF CONDITION ? THEN : ELSE

    use strict; use warnings; my $scalar; # starts empty ie undef # IF THEN + ELSE print "\$scalar now holds the following value: [", $scalar ? $scalar : + 'UNDEF',"]\n"; # oops more correct as afoken pointed below: print "\$scalar now holds the following value: [", defined $scalar ? $ +scalar : 'UNDEF',"]\n"; # update: # infact, unless you check with 'defined' a zero can mislead you: $scalar = 0; print "\$scalar now holds the following value: [", $scalar ? $scalar +: 'UNDEF',"]\n"; print "\$scalar now holds the following value: [", defined $scalar ? $ +scalar : 'UNDEF',"]\n";

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      print "\$scalar now holds the following value: [", $scalar ? $scalar : 'UNDEF',"]\n";

      It seems a defined has managed to escape from your posting. ;-)

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Undefined scalar
by thanos1983 (Parson) on Jul 11, 2017 at 16:30 UTC

    Hello Nicpetbio23!,

    Just to add something here to analytical explanation by fellow monk stevieb. You can use also the functions undef and defined.

    Usually you do not check for a scalar as my example bellow but if a scalar exists specifically in a hash (usually). But just for demonstration purposes see bellow:

    #!/usr/bin/env perl use strict; use warnings; my $scalar = undef; if (defined $scalar) { print "\$scalar is defined\n"; } else { print "\$scalar is undefined\n"; } __END__ $ perl test.pl $scalar is undefined

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!