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

ls | perl -lne '$o = $_; s/DOTX/DOT_TEST/; $n = $_; warn "***$o, $n"'
However, $o and $n are both the same. I want to do a rename($o, $n) but it won't work if both variables are the same. So it seems that a reference is being made to $_instead of a copy being put in $o.

Any idea why?

Replies are listed 'Best First'.
Re: [perlrun; perlre] $_ seems aliased instead of copied in assignment... why?
by chromatic (Archbishop) on Oct 23, 2002 at 23:20 UTC

    Simpler explanation: your substitution fails on certain lines. Remember the rule, Without error checking and sample data, it's your bug -- not Perl's!.

Re: [perlrun; perlre] $_ seems aliased instead of copied in assignment... why?
by thelenm (Vicar) on Oct 23, 2002 at 17:31 UTC
    It works fine for me on 5.6.1 and 5.8.0 (i.e., $o and $n are different if the substitution succeeds).

    -- Mike

    --
    just,my${.02}

Re: [perlrun; perlre] $_ seems aliased instead of copied in assignment... why?
by Aristotle (Chancellor) on Oct 25, 2002 at 23:21 UTC
    That will break on files with spaces, newlines or other funny characters in their name. Try perl -e 'for(@ARGV) { my $o = $_; s/DOTX/DOT_TEST/ && warn "*** $o, $_" }' * It also saves a process.

    Makeshifts last the longest.