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.
  • Is this the perl-ish way to pass arguments by-value rather than by-alias?
    • Bingo! I needed to delete select pairs of items from an import list, but I couldn't do this:

      # we don't simply splice out because we're iterating by index
      @_[ $i, $i + 1 ] = (undef, undef) if $some_condition;
      # and later
      @_ = grep { defined } @_;

      The array slice assignment gave me a "modification of read-only value" error, hence the @_ = @_; statement.

      It all feels rather clumsy, but it works.

      • Without more code for context, it's hard to see why you need to assign to @_. Why not "my @list = @_" and then work on that more-readable array?
        • It's because I'm subclassing another module which imports and I do and old trick:

          goto &Some::SuperClass::import;

          This doesn't update caller so I have no worries about whether or not the superclass checks the calling package and gets it wrong. However, it requires that @_ be present. By copying that variable to a separate array, I'd essentially be doing this:

          my @array = @_;
          # diddle @array
          @_ = @array;

          I didn't see any value in introducing a temporary variable.

          • You can’t adjust your cursor(s) to account for the missing elements?

            (Btw, I guessed that it was for de-aliasing before I read the comments saying so.)

      • why not just splice(@_, $i, 2) (or probably splice(@_, $i, 2, undef, undef), if you really need these undefs)?
        • I can't do the first because while I'm doing that, I'm iterating over the array by index. By using splice, I alter where everything is in the array and the indices are off. As for the second, I just didn't think about that. The array slice is what occurred to me first.

      • I sometimes do this:
        for (@_ = @_) {
            # Now it's safe to modify $_
            # and still leave the original passed arguments intact
            # for example:
            tr/ \n\t/ /s;
        Sometimes you just don't need a new array.