rovf has asked for the wisdom of the Perl Monks concerning the following question:

Just by chance (because Perl is full of wonders), is it possible to set the command line option -l (add implicit newline after each print) as kind of a pragma for just one block, i.e.

# .... part of my program { print "foo"; # prints "foo" } { use ??somepragma??; print "bar"; # automagically prints "bar\n" }
Don't get me wrong: I know that it is absolutely trivial to do without such a feature, but in the case it is available, I would find it useful...

-- 
Ronald Fischer <ynnor@mm.st>

Replies are listed 'Best First'.
Re: Setting -l for a block
by moritz (Cardinal) on Jan 08, 2009 at 12:55 UTC
    There's a neat trick if you want to find out what an option does, in terms of perl code: use B::Deparse:
    $ perl -MO=Deparse -le 'print 1' BEGIN { $/ = "\n"; $\ = "\n"; } print 1; -e syntax OK $ perl -MO=Deparse -ne 'print 1' LINE: while (defined($_ = <ARGV>)) { print 1; } -e syntax OK

    This shows how to get the semantics of -l and -n.

Re: Setting -l for a block
by ikegami (Patriarch) on Jan 08, 2009 at 12:52 UTC

    -l simply sets $\ so all you need is

    { local $\ = "\n"; ... }

    Be warned that local is dynamically scoped, which means that it will also affect functions called from within the block.

Re: Setting -l for a block
by JavaFan (Canon) on Jan 08, 2009 at 19:19 UTC
    Alternatively, you can use 5.10 and use 'say', which is a print with a newline. Or use Perl6::Say.