Slash Boxes
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.
More | Login | Reply
Loading... please wait.
  • I think this will do it:

    sub get_log { return splice @log }

    Just make sure you use it in list context, though.

    -- dagolden

    • Lovely!

      You don't even need the return:

      sub get_log { splice @log }

      That is a hack of great beauty. Thanks David :)

      • I only stuck the return in because I thought you were a Perl::Critic kinda guy. ;-)

        -- dagolden
        • I have mixed feelings about Perl::Critic. In any event, today I'm a do whatever feels good kinda guy :)
          • Perl::Critic is a Perl "advisor" for me. I look at what it spits out and decide on my own if I want to "conform". I have PBP on my desk so I can read up on why it wants me to do it a certain way.
  • This isn’t better, but in the spirit of TMTOWTDI and of the select idiom from perldoc -q unbuffer

    sub get_log { @{ ( [@log], @log=() )[0] } }

    Or to do away with the array-copy, assuming you have set up with “my $log = []” instead:

    sub get_log { @{ ( \@$log, $log=[] )[0] } }

    The \@$ construction is there to force Perl to make a new copy of the array reference in $log. That way the first list element is a copy of $log instead of an alias, and therefore unaffected by the overwrit

  • You can also do this with delete instead of splice:

    sub get_log { delete @log[0 .. $#log] }

    It's slightly slower than splice but you may consider it's closer to documenting your intent.

  • It does it all for you, and provides a nice interface to grabbing function arguments.