Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re^2: Using a scalar as a constant?

by Anonymous Monk
on Oct 15, 2008 at 13:35 UTC ( #717223=note: print w/replies, xml ) Need Help??

in reply to Re: Using a scalar as a constant?
in thread Using a scalar as a constant?

Correct, It is a constant but I'm retrieving it via XML so I suspect something is wrong with the XML format...

Replies are listed 'Best First'.
Re^3: Using a scalar as a constant?
by gone2015 (Deacon) on Oct 15, 2008 at 15:42 UTC

    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://717223]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2023-02-04 21:45 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (31 votes). Check out past polls.