Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Using a scalar as a constant?

by JavaFan (Canon)
on Oct 15, 2008 at 13:21 UTC ( #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?

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2023-02-09 00:19 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (44 votes). Check out past polls.