[ic] Possible minor bug (?): '-' and $Scratch->{var-name}

Ed LaFrance interchange-users@interchange.redhat.com
Fri Aug 24 17:21:00 2001


At 04:47 PM 08/24/2001 -0400, you wrote:
>Quoting Ed LaFrance (edl@newmediaems.com):
> > Hi all -
> >
> > Working on some code in an IC 4.6.5 catalog, I found that I could set and
> > view the contents of a scratch variable which contains a dash in its name
> > using IC tags:
> >
> > [set var-name]something[/set]
> > [scratch var-name]
> >
> > but operations failed within a Perl block:
> >
> > [set var-name][/set]
> > [perl]
> >          $Scratch->{var-name} = "foo";
> >          return;
> > [/perl]
> > [scratch var-name]
> >
> > In the latter example, var-name remains empty; no message in the error
> > log.  Removing the dash from var-name fixes the problem instantly.  It 
> also
> > fixes the problem to enclose the var-name in single quotes:
> >
> > [perl]
> >          $Scratch->{'var-name'} = "foo";
> >          return;
> > [/perl]
> >
> > ...and furthermore, using an underscore instead of a dash works. This is
> > pretty minor, but has me wondering; is this a side-effect of code intended
> > for other purposes or an undocumented character usage restriction?
>
>It is a Perl thing, no more, no less. '-' is a minus sign, and not
>a word character. You can only use word characters in variable names
>unless you quote them. (Knowledgeable Perl people will know the one
>exception -- when you prepend the word with a - sign and have no leading word
>chars.)
>
>Remember, this has nothing to do with any other reference -- for instance,
>you can do:
>
>         $label = 'var-name';
>         return $Scratch->{$label};
>
>That is why I rarely if ever use - in a name. Any places you see it,
>the code came from somewhere else.
>

Hmmm... I understand about character restrictions on perl variable names, 
but I thought that "$Scratch" was a reference to the scratch variable hash, 
and that Scratch->{something} retrieves a hash element, not a variable.  It 
works in my lame little test script:

#!/usr/bin/perl
my %hash;
my $hashref = %hash;
$hashref->{foo-bar} = 'test';
print "foo-bar: $hashref->{foo-bar}\n";


I guess this goes deeper.

- Ed L.



===============================================================
New Media E.M.S.               Software Solutions for Business
463 Main St., Suite D          eCommerce | Consulting | Hosting
Placerville, CA  95667         edl@newmediaems.com
(530) 622-9421                 http://www.newmediaems.com
(866) 519-4680 Toll-Free       (530) 622-9426 Fax
===============================================================