Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Using a scalar as a constant?

by JavaFan (Canon)
on Oct 15, 2008 at 13:21 UTC ( [id://717216]=note: print w/replies, xml ) Need Help??


in reply to Using a scalar as a constant?

Isn't wxTE_MULTILINE already a constant? That is, a sub returning a fixed value.

Replies are listed 'Best First'.
Re^2: Using a scalar as a constant?
by Anonymous Monk on Oct 15, 2008 at 13:35 UTC
    Correct, It is a constant but I'm retrieving it via XML so I suspect something is wrong with the XML format...

      What you're retrieving is the string with the value 'wxTE_MULTILINE', which also happens to the same as the name of the constant wxTE_MULTILINE, whose value we don't know.

      In order to set a scalar to the right value to use in place of the constant, you need to map from the string through to the value of the constant. Something along the lines of:

      sub map_constant_name { my ($name) = @_ ; # Let's not eval anything other than a simple name ! if ($name !~ /^\w+$/) { $@ = "will not map '$name'" ; return undef ; } ; # OK -- eval to try to get constant value my $val = eval "$name()" ; return $@ ? undef : $val ; } ;
      will do the trick, but is still pretty dangerous -- allowing the contents of the XML to run any subroutine visible at this point.

      Safer would be to construct a hash mapping allowed names to their values:

      my %constant_map = map { ($_, map_constant_name($_)) } qw(....) ;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://717216]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-03-28 15:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found