Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

s/// operator and binding

by ibanix (Hermit)
on Jan 28, 2003 at 00:13 UTC ( [id://230416]=perlquestion: print w/replies, xml ) Need Help??

ibanix has asked for the wisdom of the Perl Monks concerning the following question:

Hi fellow monks,

How do I do a substitute on a variable and assign it to a another variable, w/o actually editing the first variable?

This is what I've been trying
my ($path) = $logfilepath =~ s/$ref_srv/$server/i;
but this returns "1" into $path; which is suppose is the return value of the s/$ref_srv/$server/i operation.

I'll be reading perldoc, but perhaps someone will have a quicker/better/shorter way to do this.

Thanks,
ibanix

$ echo '$0 & $0 &' > foo; chmod a+x foo; foo;

Replies are listed 'Best First'.
Re: s/// operator and binding
by Ovid (Cardinal) on Jan 28, 2003 at 00:25 UTC

    Personally, I feel the answer is rather ugly and I would avoid it. You may think it's okay, though.

    (my $path = $logfilepath) =~ s/$ref_srv/$server/i;

    If $ref_srv has characters which might have a special meaning in a regex and you wish to avoid this:

    (my $path = $logfilepath) =~ s/\Q$ref_srv\E/$server/i;

    Cheers,
    Ovid

    New address of my CGI Course.
    Silence is Evil (feel free to copy and distribute widely - note copyright text)

      D'OH! That's so obvious!

      Thanks. Why do you find it ugly? Is there a better way to do it?

      Cheers,
      ibanix

      $ echo '$0 & $0 &' > foo; chmod a+x foo; foo;

        I don't want to answer for Ovid but I especially dislike mixing the declaration with that kind of assignment. I'd prefer

        my $foo; ($foo = $bar) =~ s/baz/qux/;
        but if you are going to separate them anyway, it makes perfect sense to do it in a way that makes all the steps blindingly obvious such as this:
        my $foo = $bar; $bar =~ s/baz/qux/;
        There's rarely a good reason to sacrifice clarity for brevity.

        -sauoq
        "My two cents aren't worth a dime.";
        
        Well it's ugly to me because it's not immediately apparent what you're trying to do. Why not just do this
        my($path) = $logfilepath; $path =~ s/$ref_srv/$server/i;
        Choosing between the two, I'd go with the simpler. Just because you can do something doesn't mean you should ;)

        update (broquaint): changed <pre> to <code>

        For me, this is an aesthetic thing that you can safely ignore. If I want extra parentheses, I'd code in Lisp :)

        If you like them, however, it's not a problem, so don't worry about my blather.

        Cheers,
        Ovid

        New address of my CGI Course.
        Silence is Evil (feel free to copy and distribute widely - note copyright text)

Re: s/// operator and binding
by Aristotle (Chancellor) on Jan 28, 2003 at 01:40 UTC
    s/$ref_srv/$server/i for my $path = $logfilepath;

    Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://230416]
Approved by pfaut
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-18 22:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found