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.
  • rubyisms.pm has an idiomatic translation of "yield" (As well as "self" and "super"). As for perldoc -f, ri should do everything you want.
    • Actually, I can't seem to use your 'yield' as a direct translation. I'm missing something fundamental. I put together this in Ruby (very similar to my actual problem):

      class Foo
          attr_reader :val
          def initialize(val)
              @val = val
          end
          def inc(some_val)
              @val = @val + some_val
          end
      end

      def testy(args)
          foo = Foo.new(1)
          _testy(args, foo) {
              yield foo
          }
      end

      def _testy(args,somefoo)
          for item in args
              somefoo.inc(item)
              yield
          end
      end

      testy([1,2,3]) {|x| puts x.val}

      That, as expected, prints 2, 4 and 7. However, attempting to translate that to Perl is not only exceedingly clumsy, it also doesn't seem to work and I can't figure out why.

      #!/usr/bin/env perl

      use strict;
      use warnings;

      use rubyisms;

      package Foo;
      sub new { bless { val => $_[1] } => $_[0] }
      sub inc { $_[0]->{val} += $_[1] }
      sub val { shift->{val} }
      package main;

      sub _testy(&@);

      sub testy (&@) {
          my ( undef, $args ) = @_;
          my $foo  = Foo->new(1);
          _testy { yield $foo } ($args, $foo);   # line 19
      }

      sub _testy (&@) {
          my ( undef, $args, $somefoo ) = @_;
          for (@$args) {
              $somefoo->inc($_);
              yield;
          }
      }

      testy { print shift->val, $/ } ([1,2,3]);
      __END__
      no block given (LocalJumpError) at yield.pl line 19