How best to take a copy of a variable and edit it with s///?

H

Henry Law

I frequently have the need to take copy of a variable and edit it (for
printing or some such). I code it like this:

my $ugly_variable = some_function(); # Or read from a file maybe
my $nice_variable = $ugly_variable; # Take a copy ...
$nice_variable =~ s/nasty/nice/g; # ... and then make it look nice

Of course that works very well. But it doesn't look perlish, and it
takes two statements. I'm looking for some one-shot construction, along
the lines of this

my $nice_variable = ($ugly_variable =~ s/nasty/nice/g);

.... but if course that doesn't do what I want: $nice_variable ends up as
the number of substitutions done by s///. But is there a neater, more
succinct (but not hard to maintain) way of doing what I want? I'm sure
I saw something that looked like my wrong example above, but it must
have been different in some subtle way.

Of course I Googled for an answer to this; it's not that I found
nothing, more that I couldn't structure a query that returned anything
useful.
 
D

Dave B

Henry said:
I frequently have the need to take copy of a variable and edit it (for
printing or some such). I code it like this:

my $ugly_variable = some_function(); # Or read from a file maybe
my $nice_variable = $ugly_variable; # Take a copy ...
$nice_variable =~ s/nasty/nice/g; # ... and then make it look nice

Of course that works very well. But it doesn't look perlish, and it
takes two statements. I'm looking for some one-shot construction, along
the lines of this

my $nice_variable = ($ugly_variable =~ s/nasty/nice/g);

... but if course that doesn't do what I want: $nice_variable ends up as
the number of substitutions done by s///. But is there a neater, more
succinct (but not hard to maintain) way of doing what I want? I'm sure
I saw something that looked like my wrong example above, but it must
have been different in some subtle way.

Of course I Googled for an answer to this; it's not that I found
nothing, more that I couldn't structure a query that returned anything
useful.

Since assignment returns a valid lvalue, you have to do this:

($nice_variable=$ugly_variable) =~ s/nasty/nice/g
 
H

Henry Law

Since assignment returns a valid lvalue, you have to do this:
($nice_variable=$ugly_variable) =~ s/nasty/nice/g

Of course! It's so obvious now. Thank you.
 
T

Tad J McClellan

Henry Law said:
I frequently have the need to take copy of a variable and edit it (for
printing or some such). I code it like this:

my $ugly_variable = some_function(); # Or read from a file maybe
my $nice_variable = $ugly_variable; # Take a copy ...
$nice_variable =~ s/nasty/nice/g; # ... and then make it look nice

Of course that works very well. But it doesn't look perlish, and it
takes two statements. I'm looking for some one-shot construction, along
the lines of this

my $nice_variable = ($ugly_variable =~ s/nasty/nice/g);


You have the parenthesis in the wrong place:

(my $nice_variable = $ugly_variable) =~ s/nasty/nice/g;
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top