Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Tie::File doesn't save

by nathanvit (Beadle)
on Aug 02, 2003 at 18:09 UTC ( [id://280308]=perlquestion: print w/replies, xml ) Need Help??

nathanvit has asked for the wisdom of the Perl Monks concerning the following question:

Hello, i'm working with Tie:File module.
I open a file and i push a lot of records in it. They are visible if i print all the array but when i untie it, records are not saved... So i open it as READWRITE as cpan explains in Tie/File.pm but it is the same; any ideas?
PS:I have privilege fo writing on the disk and i also tried flush funcition
my @Card=Leggi($filecard); .... push(@Card,"BEGIN:VCARD"); push(@Card,"FN:$Nuovo->{'FN:'}"); push(@Card,"N:$Nuovo->{'N:'}"); push(@Card,"END:VCARD"); ... Uscita($filecard,\@Card); sub Leggi{ my @Card; tie @Card, 'Tie::File', $filecard, mode => O_RDWR; .... return @Card; } sub Uscita{ my $filecard=shift; my $Card=shift; untie @$Card; ... exit 0; }

-­--
os: Linux Mandrake 9.0

edited by ybiC: s/Tie:/Tie::/ for searchability

Replies are listed 'Best First'.
Re: Tie:File doesn't save
by bobn (Chaplain) on Aug 02, 2003 at 19:26 UTC

    If the output file doesn't already exist, your code silently fails to tie the file,

    From perldoc Tie::File

    # create the file if it does not exist use Fcntl 'O_RDWR', 'O_CREAT'; tie @array, 'Tie::File', $file, mode => O_RDWR | O_CREA +T;

    And had you checked the return value from tie, as in:

    tie (@Card, 'Tie::File', $filecard, mode => O_RDWR ) or die $!;
    You would have seen:
    No such file or directory at tiefile.pl line 26.
    which is why we should always check the return code of anything thing that interacts with the system..

    Update: There is also a scope issue - having my @Card inside the sub Leggi make this program fail sileltnly even if the tie succeeds. My guess is that return @Card returns a list which still isn't tied. I think that either puttting the my @Card in the outer scope or returning a reference from the sub (and dealing with it in the code that invokes the sub) would fix this, though I've only tested the first of these options, although you also ahve to remove the @Card = in the @Card = Leggi($filecard); - since the tie already references the array, thi swas unneeded and complicates stuff. Anyhow, since you only posted code fragments, it's hard to test this stuff. In the future, it would help to ensure that everything between <code></code> tags compiles.

    --Bob Niederman, http://bob-n.com
Re: Tie::File doesn't save
by planetscape (Chancellor) on Jul 07, 2011 at 13:55 UTC
    They are visible if i print all the array but when i untie it, records are not saved...

    Also insure you are using the correct recsep for your filesystem.

    On Windows, I needed:

    use Fcntl 'O_RDWR'; use Tie::File; my $file = "./filename.ext"; tie (my @lines, 'Tie::File', $file, mode => O_RDWR,
    recsep => "\n"
    ) or die "Can't update $file: $! "; shift @lines; (tied @lines)->flush; untie @lines;

    for Strawberry Perl (This is perl, v5.10.0 built for MSWin32-x86-multi-thread).

    While on the same system, using Cygwin Perl (This is perl, v5.10.1 (*) built for i686-cygwin-thread-multi-64int), this did the trick:

    use Fcntl 'O_RDWR'; use Tie::File; my $file = "./filename.ext"; tie (my @lines, 'Tie::File', $file, mode => O_RDWR) or die "Can't upda +te $file: $! "; shift @lines; (tied @lines)->flush; untie @lines;

    Both were operating on the same file, which file described as:

    $ file filename.ext filename.ext: ASCII text

    HTH,

    planetscape

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2024-04-24 13:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found