Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
P is for Practical
 
PerlMonks  

Yaml !perl/range to perl range

by mpegjohn (Initiate)
on Dec 01, 2012 at 20:34 UTC ( #1006640=perlquestion: print w/ replies, xml ) Need Help??
mpegjohn has asked for the wisdom of the Perl Monks concerning the following question:

I have read in a yaml file where we specified a perl range like my_range: !perl/range 0..4 We use Yaml::XS to read in the yaml. and we get $my_range which is a reference to a perl/range which is a reference to a scalar with a value '0..4'. I have de-referenced this and tried to use this to setup an array: @array={$$value), but all I get is an array with one element of value '0..4'. How do I setup an array with this? Thanks.

Comment on Yaml !perl/range to perl range
Download Code
Re: Yaml !perl/range to perl range
by choroba (Abbot) on Dec 01, 2012 at 20:53 UTC
    Use split to get the margins:
    use strict; use warnings; use feature 'say'; my $s = "2..5"; my ($start, $end) = split /\.\./, $s; say for $start .. $end;
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Yes, that looks good. Just thought I could have used the scalar and setup an array using that scalar. I like the split idea though.
Re: Yaml !perl/range to perl range
by BrowserUk (Pope) on Dec 01, 2012 at 22:53 UTC

    As this is coming from a known format, you could safely do:  @array = eval $$value;

    $s = '1..4'; $r = \$s; @a = eval $$r; print @a;; 1 2 3 4

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2014-04-16 12:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (425 votes), past polls