in reply to Need help with s///

The substitution is never happening, that's why. And there must be something else you omitted, because as shown, your code does not run because the $NETOBJ variable is not declared anywhere. Removing the "use strict" makes it run, and running it with the -w flag (always use -w and strict!) shows you the problem:
Name "main::NETOBJ" used only once: possible typo at t.pl line 6. Use of uninitialized value in scalar dereference at t.pl line 6. Use of uninitialized value in concatenation (.) at t.pl line 6.
Ok, here's what is happening: The solution if you want the literal string '$$NETOBJ' in $a is to use single quotes instead of double quotes. The single quotes prevent interpolation. Like this:
$a='$$NETOBJ.parameter';

--ZZamboni

Replies are listed 'Best First'.
RE: Re: Need help with s///
by Tii (Monk) on Jul 15, 2000 at 07:01 UTC
    Thanks to everyone for their helpful responses.
    Actually, I was trying to keep my post brief, but I ended up leaving out too much information and still getting helpful information, but not the exact information I was seeking.

    Ok, here's the story:
    I'm parsing a config file and I'm running into a parameter that has a value like $$NETOBJ.parameter. I want to remove the $$NETOBJ. part and be left with parameter. The code I submitted earlier was a little test that I had put together, but I mistyped it. Here is my test program as I was using it:

    #!/usr/local/bin/perl $a = "$$NETOBJ.parameter"; $a =~ s/\$\$NETOBJ\.//; print $a;
    Earlier, I typed use strict; out of habit. I usually use it, but left it out for this small example.
    What the code above gives me is .parameter. My question is: Why doesn't my regexp remove the dot? I don't need to know how to create the value because I'm already reading it from the config file. I need to know how to remove the dot.

    Sorry again for not being more specific. Thank you all for your feedback.

    Tii

      The regex doesn't remove the dot because the regex doesn't match. Let's make two small changes:
      #!/usr/local/bin/perl -w $a = "$$NETOBJ.parameter"; print ">>$a<<\n"; $a =~ s/\$\$NETOBJ\.//; print $a;
      You might also write it this way:
      $a = "$$NETOBJ.parameter"; if ($a =~ s/\$\$NETOBJ\.//) { print $a; } else { print "Substitution failed.\n"; }
      One other approach that may work for you is split:
      $a = '$$NETOBJ.parameter'; (undef, $a) = split(/\./, $a, 2); print $a;
      Of course, notice the single quotes in the declaration that time. That'll help even if you do use the regexp.