ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> activeperl
activeperl
Re: Help with $_
by Olivier Mengue other posts by this author
Oct 28 2009 3:40AM messages near this date
Help with $_ | Re: Help with $_
2009/10/28 Barry Brevik <BBrevik@[...].com> 

>  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
Thread:
Barry Brevik
Olivier Mengue
Michael Ellery
Gabor Szabo

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved