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

Stucked in YAML::Tiny

by ckj (Chaplain)
on Jul 16, 2012 at 06:42 UTC ( #981973=perlquestion: print w/ replies, xml ) Need Help??
ckj has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, two days back when I ran this code it was working fine but today when I am running the same code it's not working.

My yaml file i.e. test.yaml:

rootproperty: blah section: one: two three: four Foo: Bar empty: ~

My perl code:

use YAML::Tiny; # Create a YAML file my $yaml = YAML::Tiny->new; # Open the config $yaml = YAML::Tiny->read( 'D:\Perl\bin\test.yaml' ); # Reading properties my $root = $yaml->[0]->{rootproperty}; my $one = $yaml->[0]->{section}->{one}; my $Foo = $yaml->[0]->{section}->{Foo}; # Changing data $yaml->[0]->{newsection} = { this => 'that' }; # Add a section $yaml->[0]->{section}->{Foo} = 'Not Bar!'; # Change a value delete $yaml->[0]->{section}; # Delete a value # Add an entire document $yaml->[1] = [ 'foo', 'bar', 'baz' ]; # Save the file $yaml->write( 'D:\Perl\bin\file.conf' );

It's completely confusing that how the same code is not working today, every time ti is giving the same error:

Can't call method "write" on unblessed reference at D:\Perl\bin\yaml_t +est.pl line 23.

Comment on Stucked in YAML::Tiny
Select or Download Code
Re: Stucked in YAML::Tiny
by cavac (Chaplain) on Jul 16, 2012 at 06:59 UTC

    First of all, if you have two separate blocks of code you want to post, in this case a script and a data file, please use two separate code blocks.

    Meshing it all into a single file makes it much harder for me to debug this thing. Maybe for YAML data is misformatted? The line numbers in the error message don't match up with the code you posted.

    You don't use strict; use warnings;.

    You essentially don't check for errors when reading the data file.

    Other than that, at least on my computer it works, as long as a (more or less) correctly formatted input file is provided.

    "I know what i'm doing! Look, what could possibly go wrong? All i have to pull this lever like so, and then press this button here like ArghhhhhaaAaAAAaaagraaaAAaa!!!"
Re: Stucked in YAML::Tiny
by moritz (Cardinal) on Jul 16, 2012 at 07:06 UTC

    I too get that error if reading from test.yaml fails. Then $yaml silently becomes undef, the later use of $yaml->[0] autovivifies it into an array reference, and then it's not a blessed reference, and calling write on it fails.

    I suggest to use the LoadFile and DumpFile functions instead, which seem to do proper error checking.

      On using LoadFile instead of read, it gives:
      "Failed to load YAML document from 'YAML::Tiny' at yaml_test.pl line 7 +"
      UPDATE The same code is working fine on unix machine, while on windows it's giving the same error. Not getting exactly what is the reason behind this. I checked Tiny.pm but there also nothing was found. Perl code:
      use strict; use warnings; use YAML::Tiny; use Data::Dumper; # Create a YAML file my $yaml = YAML::Tiny->new; # Open the config $yaml = YAML::Tiny->read( 'test.yaml' ); #changing yaml file $yaml->[0]->{'order.create'}->{domain} = 'ckjckcjckj'; # Save the file $yaml->write('test.yaml');
      Yaml File:
      --- order.create: domain: sdfsdfsdfsdf
      The same code works in unix but gives error in windows "Can't call method "write" on unblessed reference at yaml_test.pl line 16.".
        In my code I use use YAML qw{LoadFile}, and it works fine on both Windows and *nix. YAML file example:
        --- version: 0.11 greek: - greek_plain - greek_italic csr: FontStyle: Italic: italic Position: Subscript: sub Superscript: sup gs: - EPS - PDF
        Can you try running perl debugger on your code and using s command after LoadFile call shows up to trace code inside LoadFile?
        Sorry if my advice was wrong.
Re: Stucked in YAML::Tiny
by tobyink (Abbot) on Jul 16, 2012 at 10:07 UTC
    $yaml = YAML::Tiny->read( 'D:\Perl\bin\test.yaml' ) or die YAML::Tiny->errstr;
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: Stucked in YAML::Tiny
by Anonymous Monk on Jul 16, 2012 at 13:59 UTC

      Please show the exact code you're running. The symptoms you describe sound as if you're running

      my $string = Dump(...);

      without the appropriate use YAML::Tiny 'Dump'; at the top of your program. The documentation states that just three lines above your linked part of the documentation.

        For anyone else that finds this post when searching for a solution to this issue... For me it was because I had spaces before the '---' in my config file so the yaml object wasn't being created. It would be useful to have a check and output YAML::Tiny->errstr in the example for n00bs like myself :)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2014-10-26 00:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (149 votes), past polls