Help with $_
I am aware that there are a number of Perl "operations" that will use
the system variable $_ as the default variable if one is not supplied.
Consider the following snippet (where XMLIN is a previously opened file
handle):
foreach (<XMLIN>)
{
chomp;
# Do some stuff to the contents of the line.
print;
}
OK, what I really want to do here is print the (possibly changed) line,
AND a CR/LF, but to do that, I have to add a separate print statement
like this: print "\n";
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?
Barry Brevik
_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Help with $_
I would recommend either (1) don't chomp in the first place or (2) just
do print "$_\n".
-Mike
Barry Brevik wrote:
> I am aware that there are a number of Perl "operations" that will use
> the system variable $_ as the default variable if one is not supplied.
>
> Consider the following snippet (where XMLIN is a previously opened file
> handle):
>
>
> foreach (<XMLIN>)
> {
> chomp;
>
> # Do some stuff to the contents of the line.
>
> print;
> }
>
> OK, what I really want to do here is print the (possibly changed) line,
> AND a CR/LF, but to do that, I have to add a separate print statement
> like this: print "\n";
>
> 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?
>
> Barry Brevik
> _______________________________________________
> ActivePerl mailing list
> ActivePerl [at] listserv.ActiveState.com
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>
_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Help with $_
On Wed, Oct 28, 2009 at 1:33 AM, Michael Ellery
<mikee [at] s2technologies.com> wrote:
> I would recommend either (1) don't chomp in the first place or (2) just
> do print "$_\n".
>
> -Mike
>
> Barry Brevik wrote:
>> I am aware that there are a number of Perl "operations" that will use
>> the system variable $_ as the default variable if one is not supplied.
>>
>> Consider the following snippet (where XMLIN is a previously opened file
>> handle):
>>
>>
>> =A0 foreach (<XMLIN>)
>> =A0 {
>> =A0 =A0 chomp;
>>
>> =A0 =A0 # Do some stuff to the contents of the line.
>>
>> =A0 =A0 print;
>> =A0 }
>>
>> OK, what I really want to do here is print the (possibly changed) line,
>> AND a CR/LF, but to do that, I have to add a separate print statement
>> like this: print "\n";
>>
>> 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?
>>
Starting from Perl 5.10 you can write the following
use 5.010;
say "hello world";
and it will print hello world with a newline at the end.
Gabor
http://szabgab.com/blog.html
_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Help with $_
--===============1605392431==
Content-Type: multipart/alternative; boundary=001485f792b658d0430476fc6c59
--001485f792b658d0430476fc6c59
Content-Type: text/plain; charset=ISO-8859-1
2009/10/28 Barry Brevik <BBrevik [at] stellarmicro.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
--001485f792b658d0430476fc6c59
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
<br><br><div class=3D"gmail_quote">2009/10/28 Barry Brevik <span dir=3D"ltr=
"><<a href=3D"mailto:BBrevik [at] stellarmicro.com">BBrevik [at] stellarmicro.com<=
/a>></span><br><blockquote class=3D"gmail_quote" style=3D"border-left: 1=
px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"=
>
So after all these years, I'm wondering, is there a PERLish way to add =
a<br>
"\n" in the same line of code that prints the default $_ variable=
?<br>
<br></blockquote><div><br>From perlvar:<br><br>=A0=A0=A0 IO::Handle->out=
put_record_separator EXPR<br>=A0=A0=A0 $OUTPUT_RECORD_SEPARATOR<br>=A0=A0=
=A0 $ORS<br>=A0=A0=A0 $\=A0=A0=A0=A0=A0 The output record separator for the=
print operator. If defined,<br>
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 this value is printed after the last of p=
rint's arguments.<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 Default is "=
;undef". (Mnemonic: you set $\ instead of adding "\n"<br>=A0=
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 at the end of the print. Also, it's just=
like $/, but it's what<br>
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 you get "back" from Perl.)<br><=
br>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 va=
riables it may have side effects where you do not expect. See this code:<br=
>
<br><div style=3D"margin-left: 40px;">sub foo<br>{<br>=A0=A0=A0 print "=
;Foo\n";<br>}<br><br>print "A\n";<br>foo;<br><br>{<br>=A0=A0=
=A0 local $\ =3D "...\n";<br>=A0=A0=A0 print "B";<br>=
=A0=A0=A0 foo;<br>=A0=A0=A0 print "C";<br>
}<br>print "D\n";<br></div><br>Here is the output :<br><div style=
=3D"margin-left: 40px;">A<br>Foo<br>B...<br>Foo<br>...<br>C...<br>D<br></di=
v><br></div></div>
--001485f792b658d0430476fc6c59--
--===============1605392431==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============1605392431==--