Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Yaml !perl/range to perl range

by mpegjohn (Novice)
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.

Replies are listed 'Best First'.
Re: Yaml !perl/range to perl range
by choroba (Chancellor) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1006640]
Approved by Athanasius
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2017-09-25 17:08 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (284 votes). Check out past polls.