Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

How to change the scalar value to some other context.

by santhosh_89 (Scribe)
on Jul 24, 2009 at 11:09 UTC ( #782929=perlquestion: print w/ replies, xml ) Need Help??
santhosh_89 has asked for the wisdom of the Perl Monks concerning the following question:

I want to change the scalar value to some other context(array/hash references). Actually my input will be as following.
Scalar will be a Input : '[1,'test',[{2=>3,3=>'test_56'},2,3,3]]'
If the scalar contains [],{}.I want change it to array-reference,hash-ref. The output should be as following if i print by using dumper module.
Dumper output :

$VAR1 = [ 1, 'test', [ { '3' => 'test_56', '2' => 3 }, [ 2 ], [ 3 ], 3 ] ];

Comment on How to change the scalar value to some other context.
Download Code
Re: How to change the scalar value to some other context.
by ig (Vicar) on Jul 24, 2009 at 11:20 UTC

    It is difficult to understand what your question or problem is. It might help if you reviewed How do I post a question effectively? and perhaps brian's Guide to Solving Any Perl Problem, then updated your post with a question or statement describing what is preventing you doing what you want to do.

    update: rewritten to (I hope) clarify.

    update: The eval function may be what you are looking for. Consider the following:

    use strict; use warnings; use Data::Dumper; my $x = "[1,'test',[{2=>3,3=>'test_56'},[2],[3],3]]"; my $y = eval $x; print Dumper($y);

    Is this what you are wanting to do?

      Is it secure method to use eval function.

        Is it secure method to use eval function.

        That is an excellent question. The short answer is: no.

        The eval function will parse and execute arbitrary code. Therefore, it is quite dangerous.

        Your program can still be secure if it ensures that the eval function will only evaluate "safe" code. To do this, you must carefully screen all inputs that go into the code the eval function evaluates. You might find perlsec and the 'taint' mode it describes helpful.

Re: How to change the scalar value to some other context.
by scorpio17 (Monsignor) on Jul 24, 2009 at 13:13 UTC
    You might consider using JSON instead. The format is different, but not by much. I've found JSON to be useful for "stringifying" data structures. There are modules on CPAN for encoding/decoding. And it avoids having to worry about eval.
Re: How to change the scalar value to some other context.
by Your Mother (Canon) on Jul 24, 2009 at 15:54 UTC

    I echo what ig and scorpio17 said. Don't use Dumper/eval for this. Use JSON (with JSON::XS installed so it uses it instead of the Perl version), YAML, or Storable. Those are all better, safer options.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://782929]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (11)
As of 2014-12-22 15:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (119 votes), past polls