Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • #!/usr/bin/perl

    use strict;
    use warnings;

    my @a = (
                    1,
                    @{[map {()} warn "in here\n"]},
                    2,
                    3
    );

    print map { "$_\n" } @a;

    • or the slightly less stupid (I don't know why I threw it in an arrayref to start with):

      #!/usr/bin/perl

      use strict;
      use warnings;

      my @a = (
                      1,
                      (map {()} warn "in here\n"),
                      2,
                      3
      );

      print map { "$_\n" } @a;

      • An even shorter way to write that is this:

        (map {;} warn "in here\n")

        However, I prefer to take a page from Javascript, by defining the following amusing function:

        sub void {}

        I can then write the code in this much nicer way:

        (void warn "in here\n")

        This is far nicer than Ovid’s approach as well, IMO.

  • return() with no arguments (which is what you're doing) does the "right thing" in both scalar and list contexts. It returns either an empty list or undefined value.

    Thus you could also have written:

    return;

    for the same effect. I'm sure you know this already, but from your commentary and style it did look like you were trying to explicitly return an empty list.

    • Yes, I normally just use a bare return (and get annoyed when people use 'return 0' for false), but I honestly didn't think about that here because I originally had this:

      my $warn = sub { warn Dumper($self->build_context); () };

      That showed the annoying __LINE__ problem and I decided to be explicit about the return when I added the $line variable. Normally about the only time -- aside from this example -- I return an explicit empty list is when I do this:

      return $condition ? $some_value : ();

    • Oh, and what I didn't say, but should have, in my first reply was "thanks". The bare return is (IMHO) a cleaner solution.

    • For less trickery with subs and call stacks and line numbers, remember your old friend 'do'!

      $ perl -le'print join " ", (1, do { warn 2; () }, 3)'
      2 at -e line 1.
      1 3