How to print modified version of string without intermediate variable?

U

usenet

I can do this:

my $foo = "use php";
(my $bar = $foo) =~ s/php/Perl/ig;
print $bar;

## output: 'use Perl'

But I used an intermediate variable ($bar) which I generally dislike.

How could I print the modified version of $foo without changing the
value of $foo and without needing to create an intermediate variable
for this purpose?
 
A

A. Sinan Unur

(e-mail address removed) wrote in @z14g2000cwz.googlegroups.com:
I can do this:

my $foo = "use php";
(my $bar = $foo) =~ s/php/Perl/ig;
print $bar;

## output: 'use Perl'

But I used an intermediate variable ($bar) which I generally dislike.

How could I print the modified version of $foo without changing the
value of $foo and without needing to create an intermediate variable
for this purpose?

So, you want to change the value in $foo without changing the value of
$foo.

use strict;
use warnings;

our $foo = q{use php};

{
local $foo = $foo;
$foo =~ s/php/Perl/ig;
print "$foo\n";
}

print "$foo\n";

__END__

Sinan
 
U

usenet

A. Sinan Unur said:
So, you want to change the value in $foo without changing the value of $foo.

No, not really. I want to display a modified version of $foo. This
would be analogous to using printf to display a number as a rounded
value.
local $foo = $foo;

Yeah, but it's still an intermediate variable (it's just more tightly
scoped).
 
A

A. Sinan Unur

(e-mail address removed) wrote in
A. Sinan Unur wrote:

No, not really. I want to display a modified version of $foo. This
would be analogous to using printf to display a number as a rounded
value.

It cannot be analogous to printf. The subsitution operator changes the
value of the variable that it is applied to. I do not see a way of
applying the substitution operator to a variable without changing its
value. Hence, you will need to copy that value to something.
Yeah, but it's still an intermediate variable (it's just more tightly
scoped).

I would not call it more tightly scoped given that I had to use our
$foo.

Sinan
 
U

usenet

A. Sinan Unur said:
It cannot be analogous to printf. The subsitution operator changes the
value of the variable that it is applied to. I do not see a way of
applying the substitution operator to a variable without changing its value.

Yes, of course. So the answer to my question (if such an answer
exists) wouldn't employ the substitution (s///) operator at all. It
would use some other technique to accomplish the same task.

Of course, such an alternate technique may not exist.
 
X

xhoster

Yes, that's exactly the issue. $a++ changes the value of its variable,
too. However, there is a similar operation which doesn't ($a+1). There is
no analogous non-in-place operation for substitution.
Yes, of course. So the answer to my question (if such an answer
exists) wouldn't employ the substitution (s///) operator at all. It
would use some other technique to accomplish the same task.

Of course, such an alternate technique may not exist.

As far as I know, there isn't such a technique, other than defining a
subroutine which will hide the ugly intermediate variable from your sight.
"chomp" is the same way.

Xho
 
E

Eden

(e-mail address removed) escreveu:
I can do this:

my $foo = "use php";
(my $bar = $foo) =~ s/php/Perl/ig;
print $bar;

## output: 'use Perl'

But I used an intermediate variable ($bar) which I generally dislike.

How could I print the modified version of $foo without changing the
value of $foo and without needing to create an intermediate variable
for this purpose?

An interesting challenge, here's what I figured out:

my $foo = "use php";
for("$foo") {s/php/Perl/ig; print;}

This does use an intermediate variable ($_) behind the scenes but since
its limited to the for scope and doesn't clutter anything, I suppose
this will do.
 
K

Keith Keller

An interesting challenge, here's what I figured out:

my $foo = "use php";
for("$foo") {s/php/Perl/ig; print;}

This does use an intermediate variable ($_) behind the scenes but since
its limited to the for scope and doesn't clutter anything, I suppose
this will do.

It will not do:

$ perl -e '$foo="use php";for ($foo) {s/php/Perl/ig;print;};print $foo'
use Perluse Perl

$foo is aliased to $_ inside the loop, so modifying $_ modifies $foo.

--keith
 
G

Gunnar Hjalmarsson

Keith said:
-------^----^

It will not do:

$ perl -e '$foo="use php";for ($foo) {s/php/Perl/ig;print;};print $foo'

That's not what "Eden" suggested. You removed the doublequotes.
 
B

Brian McCauley

my $foo = "use php";
(my $bar = $foo) =~ s/php/Perl/ig;
print $bar;
But I used an intermediate variable ($bar) which I generally dislike.

Yes, I find it ulgy too which is why I came up with the apply()
function (which I contributed to Tassilo's List::MoreUtils).

use List::MoreUtils qw( apply )
print apply { s/php/Perl/ig } $foo;
 
E

Eden Cardim

Brian McCauley escreveu:
Yes, I find it ulgy too which is why I came up with the apply()
function (which I contributed to Tassilo's List::MoreUtils).

use List::MoreUtils qw( apply )
print apply { s/php/Perl/ig } $foo;

This is very more readable and organized. I'd use this in a large
system. The for trick should be a tad bit faster, however.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top