http://www.perlmonks.org?node_id=930085

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

I'm trying to remove a text string using the "~s///g" syntax from a list of directory paths which is stored in an array, and it's just not working.

I googled this topic for hours, double-checked my syntax over and over, but I just don't understand what I'm doing wrong.

Before I go down the road of using "substr" & "index", I'd like to see if anyone has any input/thoughts/suggestions on this.

# List of Directories within ACE Directory Template my @ACETmpltDirs = ("./dataoper/stagecdm.aceprd/stagepgms"); my $ACETmpltDir; # ACE Environment my $ACEEnvironment = "aceprd"; # Deal with each Directory within ACE Directory Template foreach $ACETmpltDir (@ACETmpltDirs) { if (index($ACETmpltDir,".$ACEEnvironment") == -1) { print "no env: $ACETmpltDir"; } elsif (index($ACETmpltDir,".$ACEEnvironment") != -1) { print "env : $ACEEnvironment\n"; print "w/ env : $ACETmpltDir\n"; $ACETmpltDir = ~s/$ACEEnvironment//g; print "w/o env: $ACETmpltDir\n"; } }
The result makes no sense to me, and I cannot figure out what I'm doing wrong.
env : aceprd w/ env : ./dataoper/stagecdm.aceprd/stagepgms Use of uninitialized value in substitution (s///) at ./acedirs_build l +ine 192. w/o env: 18446744073709551615

Line 192 is "$ACETmpltDir = ~s/$ACEEnvironment//g;".

Any help/insight would be very much appreciated!

Replies are listed 'Best First'.
Re: Use of uninitialized value in substitution (s///)
by BrowserUk (Patriarch) on Oct 06, 2011 at 23:57 UTC
    Line 192 is "$ACETmpltDir = ~s/$ACEEnvironment//g;".

    That should be $ACETmpltDir =~ s/$ACEEnvironment//g;. Pay careful attention to the spacing around =~.

    What you have at the moment is trying to apply the substitution to $_; then apply logical not (*) bitwise negation (~) to the numeric result of that:

    $_='aaaaa'; print s[a][]g;; 5 $_='aaaaa'; print ~s[a][]g;; 18446744073709551610

    and finally assign (=) that to $ACETmpltDir, but it falls at the first hurdle because $_ is undefined.

    (*)Corrected. Thanks to AnomalousMonk


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      THANK YOU!

      I totally overlooked this part of the syntax, even from the examples I looked up online.

        Happened to me just a few hours ago :)

        That really ought to be a warning, I can't imagine a use case where you want the bitwise negation of the number of substitutions  $foo = ~s/// or matches  $foo = ~m//

        every time I've encountered this in the past, its always been a mistake

        warnings should issue a specific warning

Re: Use of uninitialized value in substitution (s///)
by armstd (Friar) on Oct 07, 2011 at 04:34 UTC

    While you're at it, unless $ACEEnvironment is a regexp, you probably also want to quote it better to prevent unintended matches...

    $ACETmpltDir =~ s/\Q$ACEEnvironment\E//g;

    It's clearly not the case here, but it's a good habit to get into.

    --Dave