Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8478279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:42:07+00:00 2026-06-10T18:42:07+00:00

When I run the below script I get Can’t use an undefined value as

  • 0

When I run the below script I get

Can't use an undefined value as a symbol reference at ./yaml-test.pl line 52.

where line 52 is

seek $fh, 0, SEEK_SET;   # seek back to the beginning of file

The purpose of the script is to reproduce why I get corrupt yaml files, so the global file handles are suppose to be there.

My theory is that newly written yaml files are not overwritten, and therefore if the newly written yaml is smaller than the old, then old data will still be in the new yaml file.

Question

Can anyone see what is wrong with my script?

#!/usr/bin/perl

use strict;
use YAML::Syck;
use Fcntl ':flock', 'SEEK_SET';
use warnings;
use Data::Dumper;

my $acc;
my $acc_fh;

$acc->{1}{name1} = "abc";


system("rm -f test.yaml");

# write initial
open F, '>', 'test.yaml';
print F YAML::Syck::Dump($acc);
close F;


$acc->{1}{name2} = "abc";

write_yaml_with_lock($acc, $acc_fh);

$acc->{1}{name3} = "abc";

($acc, $acc_fh) = read_yaml_with_lock('test.yaml');

$acc->{1}{name4} = "abc";

write_yaml_with_lock($acc, $acc_fh);



sub read_yaml_with_lock {
    my ($file) = @_;

    open my $fh, '+<', $file or die $!;
    flock($fh, LOCK_EX) or die $!;

    my $obj = YAML::Syck::LoadFile($fh); # this dies on failure
    return ($obj, $fh);
}

sub write_yaml_with_lock {
    my ($obj, $fh) = @_;

    my $yaml = YAML::Syck::Dump($obj);
    $YAML::Syck::ImplicitUnicode = 1;
    seek $fh, 0, SEEK_SET;   # seek back to the beginning of file

    print $fh $yaml . "---\n";
    close $fh;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T18:42:08+00:00Added an answer on June 10, 2026 at 6:42 pm

    You call write_yaml_with_lock() twice. The first time you call it $acc_fh is still undef because it is not set until two lines further down via read_yaml_with_lock().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I run the below script I get a corrupt yaml file like so
When I run the script below, I get $VAR1 = [ 'ok0.ok]][[file:ok1.ok', undef, undef,
i am trying to run the below script SELECT field_value FROM jos_js_res_record_values jrv INNER
When I attempt to run the script below in Eclipse(PyDev): import subprocess subprocess.call(/usr/local/bin/mitmdump) An
When I run the below code, everything goes great until I change the value
When you run perl -e "Bla->new" , you get this well-known error: Can't locate
I am using below script to get the Job_state for Agent Job. It returns
When I attempt to run automated tests via Microsoft Test Manager, I get the
Hi I cant get the below script ive worked on to pickup the extension
When I run below code, it's giving a message that: the application stopped unexcepctedlly

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.