I was fixing SPR's this morning and ran into a situation where I needed to replace text in a string used to generate a report.

So, I looked at the code, found where it needed to go and added something along these lines:

$_ = $guarantee; s/OldValue/NewSetting/; $guarantee = $_;

(I know it's not the most condensed code, but it fits my current style.)

Not really overly impressive or unusual, but serviceable. Then, I tried to compile the program and ran into all sorts of error messages.

Maybe I was still a little foggy from the early hour (I start at 6:00 am, to avoid the worst of local traffic) and all, but it took me several seconds to realize what I'd done wrong.

You see, the day job pays me to write apps in Borland Delphi, which uses ObjectPascal and contains no built-in support for regular expressions, scalars, $_, or other Perlisms.

I had to laugh...Instead of thinking in Pascal, as I have done for roughly half of my career, I was thinking Perl to work through my Pascal solution. Thanks, y'all.

--f

Update: Thanks, tilly; I'm glad to hear that. It makes more sense to me, but then I'm only an apprentice. :)
For the rest of you, if you haven't seen that thread or the one it references, I recommend both highly.

Update 2: I should mention that this was this first time I'd run into this with Perl; it's happened with other languages, but not Perl.

Replies are listed 'Best First'.
Re (tilly) 1: A minor epiphany
by tilly (Archbishop) on Feb 03, 2001 at 08:13 UTC
    Strong recommendation. Get in the habit of doing the above as:
    $guarantee =~ s/OldValue/NewSetting/; # /g?
    rather than playing games with $_. First of all it is more obvious. But secondly you will avoid the problems that I mentioned in Re: Re: $_ haters anonymou - problems that will come back and bite you when you start trying to use features like map and grep. (Which alias each element in a list to $_ and then do something. Well if the do something calls a function like the above...)
Re: A minor epiphany
by runrig (Abbot) on Feb 03, 2001 at 21:22 UTC
    If you have several things to do, you can localize $_ (Re: Re: $_ haters anonymou notwithstanding):
    # I suppose I must be in the $_ appreciation society :) for ($guarantee) { s/this/that/g; s/foo/bar/g; #..etc. } # or (probably not preferable in this case, but it IS sometimes # handy to localize $_, e.g. in File::Find 'wanted()' subroutines) { local $_ = $guarantee; # ... some operations on $_ $_ = $guarantee; }

      Just a slight expansion... The first one translates roughly to:

      { local $_; *_= \$guarantee; s/this/that/g; s/foo/bar/g; }

      which means that during that block you can take and keep a reference to \$_ and you'll get a reference to $guarantee (not that such happens often, but it is one way to distinguish between the two). (:

              - tye (but my friends call me "Tye")
Re: A minor epiphany
by TheoPetersen (Priest) on Feb 03, 2001 at 22:23 UTC
    This happens to me most every time I have to go back to C; I'll try pushing something onto an array or put type punctuation in front of variable names.

    I keep thinking we need a muddled Perl/C converter to fix up programs for Perl addicts who are pushed back into other languages.