in reply to Re^3: Data::Dumper output
in thread Data::Dumper output

To continue to use \n, use a list with print ...

Yes. Although I'd use a separate line for clarity

print Dumper $data_structure; print "\n";
I just feel that is clearer and less prone to the sort of error I created.

Replies are listed 'Best First'.
Re^5: Data::Dumper output
by afoken (Chancellor) on May 08, 2021 at 16:00 UTC
    To continue to use \n, use a list with print ...

    Yes. Although I'd use a separate line for clarity

    print Dumper $data_structure; print "\n";

    Just use say:

    say Dumper $data_structure;

    say outputs a trailing newline unless the arguments already contain a trailing newline.

    I just feel that is clearer and less prone to the sort of error I created.

    I'm no Lisp guy, but using a few more brackets helps to avoid ambiguity and misunderstandings between humans and computers. If you call a function or method, add brackets around the argument list. That's what I do, with only very few exceptions - mostly stuff that is listed in perlfunc, but behaves more like a keyword or a C macro (__FILE__, __LINE__, __PACKAGE__, do, eval, package, use, require) than like a function.

    I would have written ...

    say Dumper($data_structure);

    ... in code for perl 5.10 and newer (i.e. with use v5.10 in the file), or ...

    print Dumper($data_structure),"\n"; # or print Dumper($data_structure)."\n";

    ... in code for older perls.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      say outputs a trailing newline unless the arguments already contain a trailing newline.

      Are you sure about that? The documentation makes no mention of such a condition and this example suggests that it is not the case:

      $ cat say.pl #!/usr/bin/env perl use strict; use warnings; use feature 'say'; my $x = "foo\n"; my $y = 'bar'; say $x; say $y; $ ./say.pl foo bar $

      🦛

        say outputs a trailing newline unless the arguments already contain a trailing newline.

        Are you sure about that? The documentation makes no mention of such a condition and this example suggests that it is not the case:

        D'oh! Of course, you are right, say simply appends a newline, and that's also what's written in the documentation.

        The "add a newline unless there is already one" is my mental model of say, and my code never proved me wrong - because I never called it with a string already containing a trailing newline. And to make things worse, I even linked to the documentation in Re^5: Data::Dumper output. I wonder why my mental model of say is wrong. A blame on perlfunc states that the documentation hasn't changed for two years, and has no relevant changes since say was introduced.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)