Regex without assignment

T

Tom Gur

Hi,

is there a way for me to use a regex without assignment ?

e.g:
Lets say I have: $filepath = "/dir_a/dir_b/";
and a function: &accept_only_backslashes(); that as the name implies -
accept only backslashes

right now, the only way I can use regex to help is:
$temp = $filepath;
$filepath =~ s/\//\\/g
&accept_only_backslashes($filepath);
$filepath = $temp;

this is very ugly.
is there a way to just send the value of the substitution, without
actually changing the variable ?

Thanks,
Tom
 
P

Paul Lalli

is there a way for me to use a regex without assignment ?

e.g:
Lets say I have: $filepath = "/dir_a/dir_b/";
and a function: &accept_only_backslashes(); that as the name
implies - accept only backslashes

right now, the only way I can use regex to help is:
$temp = $filepath;
$filepath =~ s/\//\\/g
&accept_only_backslashes($filepath);
$filepath = $temp;

this is very ugly.
is there a way to just send the value of the substitution, without
actually changing the variable ?

If you perform a s/// on an assignment expression, only the new
variable is changed, not the original. So you could change the above
to:
(my $temp = $filepath) =~ s#/#\\#g;
accept_only_backslashes($temp);

$filepath will not be changed at all.

Paul Lalli
 
J

Jürgen Exner

Tom said:
is there a way for me to use a regex without assignment ?

A regular expression does not do any assignments (except for the
backreferences or capturing of course).
e.g:
Lets say I have: $filepath = "/dir_a/dir_b/";
and a function: &accept_only_backslashes(); that as the name implies -
accept only backslashes

right now, the only way I can use regex to help is:
$temp = $filepath;
$filepath =~ s/\//\\/g

The only regular expression in the line above is /\//. On top of that you
are doing a substitution and a binding. I don't see any assignment.
&accept_only_backslashes($filepath);

Do you know what the & does and are you sure you want that semantic?
$filepath = $temp;

this is very ugly.
is there a way to just send the value of the substitution, without
actually changing the variable ?

Ohhhhhhh, you were talking about the s operator the whole time, not about
regular expressions? Then why on earth were you dangling that red herring
and ranting about REs all the time?

And no, both operator, s/// as well as tr///, will modify their argument
rather then returning the new string as a result.
However you can somewhat simplify your code by tying the substitution to
$tmp and passing that string as argument to the function call. Then you
don't need that second assignment to restore the original value.

jue
 
T

Tom Gur

If you perform a s/// on an assignment expression, only the new
variable is changed, not the original. So you could change the above
to:
(my $temp = $filepath) =~ s#/#\\#g;
accept_only_backslashes($temp);

$filepath will not be changed at all.

Paul Lalli

Thanks a lot !
 

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
474,208
Messages
2,571,079
Members
47,682
Latest member
TrudiConna

Latest Threads

Top