in reply to here-docs vs quote operators

Although you're right about the quoting mechanisms, I wouldn't tell people to follow your examples. If there's a way not to use the string form of eval, take it. Creating an anonymous subroutine lets you write Perl code exactly how you should write it and without any worries about quoting. You don't have to worry about the difference in any of these.

my $hd = sub { '[]' =~ /\\[\\]/ };

In your last example, since you're already using an alternate delimiter, just choose one that doesn't show up in the data. That takes care of the syntax error quite nicely, and it's that reason we have alternate delimiters. Then you won't need three lines for a one line statement. :)

--
brian d foy <brian@stonehenge.com>
Subscribe to The Perl Review

Replies are listed 'Best First'.
Re^2: here-docs vs quote operators
by ikegami (Patriarch) on Feb 16, 2006 at 21:38 UTC
    I wholely agree. However, sub cannot always be used. My typical use for here-docs is when using Parse::RecDescent, whose input includes uncompiled code.
Re^2: here-docs vs quote operators
by Anonymous Monk on Feb 16, 2006 at 21:37 UTC
    Next, print that anonymous sub to stdout.

    If you can do it, tell me how.

      From Perl Hacks:

      my $deparse = B::Deparse->new(); print $deparse->coderef2text( $anonysub );

        Of course this and the Data::Dumper wrapper around this interface won't handle closures properly. (You know this I'm sure, but not everybody else does. :-) Which is why using Data::Dump::Streamer is IMO preferable.

        ---
        $world=~s/war/peace/g

      Use Data::Dump::Streamer to do the deparsing for you. (This is also mentioned in perl-hacks I think :-)

      use Data::Dump::Streamer; my $hd = sub { '[]' =~ /\\[\\]/ }; Dump($hd)->Names('hd')->Out(); __END__ $hd = sub { '[]' =~ /\\[\\]/; };
      ---
      $world=~s/war/peace/g

      How about this?

      use strict; use Data::Dumper; sub print_code { my ($sub) = @_; my $d = Data::Dumper->new( [$sub] ); $d->Deparse(1); print $d->Dump(); }; print_code(sub{ print 'This is a coderef' });
        Hmm... doesn't work on my system. :-( I get: Can't locate object method "Deparse" via package "Data::Dumper" (perhaps you forgot to load "Data::Dumper"?) at - line 6.

        What version of perl and/or Data::Dumper do I need to pull off that trick? :-(