2009/10/28 Barry Brevik <[email protected]>

> So after all these years, I'm wondering, is there a PERLish way to add a
> "\n" in the same line of code that prints the default $_ variable?
>
>
>From perlvar:

    IO::Handle->output_record_separator EXPR
    $OUTPUT_RECORD_SEPARATOR
    $ORS
    $\      The output record separator for the print operator. If defined,
            this value is printed after the last of print's arguments.
            Default is "undef". (Mnemonic: you set $\ instead of adding "\n"
            at the end of the print. Also, it's just like $/, but it's what
            you get "back" from Perl.)

However I would not recommend to use it (so not PERLish) as it makes the
code less explicit. Also, the problem of $\ is that as with all lexical
variables it may have side effects where you do not expect. See this code:

sub foo
{
    print "Foo\n";
}

print "A\n";
foo;

{
    local $\ = "...\n";
    print "B";
    foo;
    print "C";
}
print "D\n";

Here is the output :
A
Foo
B...
Foo
...
C...
D
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to