Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

How to make array references from scalar value

by santhosh_89 (Scribe)
on Jul 29, 2009 at 11:10 UTC ( #784200=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 make array/hash references from scalar value. I used eval method to convert to perl structure. I Used character inside the scalar value .it will not be single quote in value field as
( $a="a,b,{perl=>monk}").So the eval method returns null value.If i use single quote in all the values.Eval will return the correct value.Why the single quote is needed inside the scalar value (my $a="'a','b',{'perl'=>'monk'}" ),already The value is with in double quotes. otherwise anyone knows any cpan modules to solve this problem.The problems is if the string found [,{,I need to change that value to hash({) or array([) references.
The eval is given null value. my $a="[a,b,{perl=>monk}]"; my $b=eval $a; print Dumper($b); Following coding is working fine. my $a="['a','b',{'perl'=>'monk'}]"; my $b=eval $a; print Dumper($b);

Comment on How to make array references from scalar value
Download Code
Re: How to make array references from scalar value
by ELISHEVA (Prior) on Jul 29, 2009 at 11:25 UTC

    You need the single quotes inside the double quotes because the stuff inside the double quotes must be valid Perl code. If your module or script has use strict, [a,b,{perl=>monk}] is not a valid expression. But ['a','b',{perl=>'monk'}] and ['a','b',{'perl'=>'monk'}] are.

    Best, beth

Re: How to make array references from scalar value
by Anonymous Monk on Jul 29, 2009 at 11:26 UTC
    null? you mean undef. You're supposed to show your real code, because the code you've shown works as you expect
    #!/usr/bin/perl -- use Data::Dumper; { my $a="[a,b,{perl=>monk}]"; my $b=eval $a; warn $@ if $@; print Dumper($b); } { my $a="['a','b',{'perl'=>'monk'}]"; my $b=eval $a; warn $@ if $@; print Dumper($b); } { use strict; use warnings; my $a="[a,b,{perl=>monk}]"; my $b=eval $a; warn $@ if $@; print Dumper($b); } __END__ $VAR1 = [ 'a', 'b', { 'perl' => 'monk' } ]; $VAR1 = [ 'a', 'b', { 'perl' => 'monk' } ]; Bareword "a" not allowed while "strict subs" in use at (eval 3) line 1 +. Bareword "b" not allowed while "strict subs" in use at (eval 3) line 1 +. Bareword "monk" not allowed while "strict subs" in use at (eval 3) lin +e 1. $VAR1 = undef;
    It only "breaks" under strict subs (my last example)
Re: How to make array references from scalar value
by moritz (Cardinal) on Jul 29, 2009 at 11:28 UTC
    If you print $a in first version, it says a, b, {perl => monk }. If you eval it, it is interpreted as a perl program.

    In a perl program with strict in effect, a, b and monk are forbidden. The eval doesn't care how you constructed the string, it just works with what it sees.

Re: How to make array references from scalar value
by Anonymous Monk on Jul 29, 2009 at 11:39 UTC
    #!/usr/bin/perl -- use strict; use warnings; use Safe; my $result = Safe->new->reval("[a,b,{perl=>monk}]"); use Data::Dumper; print Dumper($result); __END__ $VAR1 = [ 'a', 'b', { 'perl' => 'monk' } ];
    Safe Undumping
      Is it secure method to use this package and reval method in real time project.

      I checked the Safe modules from cpan.It has some bugs.It will cause the memory problem and it doesn't work under 5.5.5 version with version.pm.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2014-08-01 01:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (256 votes), past polls