Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Yaml !perl/range to perl range

by mpegjohn (Novice)
on Dec 01, 2012 at 20:34 UTC ( [id://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 (Cardinal) 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 (Patriarch) 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
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-04-20 02:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found