Help with $_

Help with $_

am 28.10.2009 00:27:59 von Barry Brevik

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 ()
{
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@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Help with $_

am 28.10.2009 00:33:25 von Michael Ellery

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 ()
> {
> 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@listserv.ActiveState.com
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Help with $_

am 28.10.2009 08:42:58 von Gabor Szabo

On Wed, Oct 28, 2009 at 1:33 AM, Michael Ellery
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 ()
>> =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@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Help with $_

am 28.10.2009 11:40:28 von olivier.mengue

--===============1605392431==
Content-Type: multipart/alternative; boundary=001485f792b658d0430476fc6c59

--001485f792b658d0430476fc6c59
Content-Type: text/plain; charset=ISO-8859-1

2009/10/28 Barry Brevik

> 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



2009/10/28 Barry Brevik "><BBrevik@stellarmicro.com<=
/a>>

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

"\n" in the same line of code that prints the default $_ variable=
?



From perlvar:

  =A0 IO::Handle->out=
put_record_separator EXPR
  =A0 $OUTPUT_RECORD_SEPARATOR
  =
=A0 $ORS
  =A0 $\    =A0 The output record separator for the=
print operator. If defined,

          =A0 this value is printed after the last of p=
rint's arguments.
          =A0 Default is "=
;undef". (Mnemonic: you set $\ instead of adding "\n"
=A0=
           at the end of the print. Also, it's just=
like $/, but it's what

          =A0 you get "back" from Perl.)
<=
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: >

sub foo
{
  =A0 print "=
;Foo\n";
}

print "A\n";
foo;

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

}
print "D\n";

Here is the output :
=3D"margin-left: 40px;">A
Foo
B...
Foo
...
C...
D
v>


--001485f792b658d0430476fc6c59--

--===============1605392431==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============1605392431==--