[Cialug] perl problem

Renegade Muskrat dramaley at spatulacity.cx
Thu Feb 9 17:41:29 CST 2006


Thanks to everyone who responded to my Perl question. I figured it out, 
from what was said here.

Perl's symbolic references always refer to variables that are in the 
symbol table (aka, globals). I was able to use hard references to 
accomplish the same task (and it even works with "use strict 'refs'"):


#!/usr/bin/perl
use strict;
use warnings;

our $foo = "global foo";

{
     my $foo = "local foo";
     my $bar = "local bar";

# This next line is what changed:
     my $test1 = join("\n", map { $$_; } (\$foo, \$bar));
     my $test2 = join("\n", $foo, $bar);
     print "$test1\n----------\n$test2\n";
}


output:


local foo
local bar
----------
local foo
local bar


I am aware that using "keys" without "sort" returns results in a 
seemingly-random order, but in my case the ordering won't be important.

In case anyone is really wondering why i asked in the first place, it 
was because i found some really ugly code in a program i'm rewriting:

if (($PLATFORM =~ /$a1$FORM{'query'}$a2/io)
     || ($DATE =~ /$a1$FORM{'query'}$a2/io)
     || ($IP =~ /$a1$FORM{'query'}$a2/io)
     || ($USER =~ /$a1$FORM{'query'}$a2/io)
     || ($MAC =~ /$a1$FORM{'query'}$a2/io)) {
     $USER =~ s/$a1($FORM{'query'})$a2/<strong>$1<\/strong>/io;
     $MAC =~ s/$a1($FORM{'query'})$a2/<strong>$1<\/strong>/io;
     $PLATFORM =~ s/$a1($FORM{'query'})$a2/<strong>$1<\/strong>/io;
     $DATE =~ s/$a1($FORM{'query'})$a2/<strong>$1<\/strong>/io;
     $IP =~ s/$a1($FORM{'query'})$a2/<strong>$1<\/strong>/io;

I find my version much easier to read:

if (grep { $$_ =~ s/$a1($FORM{'query'})$a2/<strong>$1<\/strong>/io }
          (\$DATE, \$IP, \$MAC, \$PLATFORM, \$USER)) {



More information about the Cialug mailing list