Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Tie::File is driving me crazy

by Dandello (Scribe)
on Feb 01, 2011 at 18:24 UTC ( #885584=perlquestion: print w/ replies, xml ) Need Help??
Dandello has asked for the wisdom of the Perl Monks concerning the following question:

I have a script that will read from a Tied file without problems.

But I'm missing something painfully obvious when trying to write to a tied file.

#!/usr/bin/perl ###################################################################### +############ use strict; #use warnings; use Tie::File; popfileb(); print qq{DONE!!\n} or die 'unable to print to screen'; my @aod=(); my @aox=(); tie @aod, 'Tie::File', 'tmp/test.txt', recsep => "\n"; sub popfileb { foreach my $yb ( 0 .. 10 ) { foreach my $xb ( 0 .. 10 ) { #set first row; if ( $yb == 0 ) { substr $aox[0], $xb, 1, 'n'; } #set increasing rows; elsif ( $yb > 0 ) { substr $aox[$yb], $xb, 1, 'a'; } } $aod[$yb] = qq{$aox[$yb]\n}; } return; } untie @aod; exit;

The script runs but doesn't output anything to the file.

I've gone through all the examples I can find but can't seem to get a handle on this.

Comment on Tie::File is driving me crazy
Download Code
Re: Tie::File is driving me crazy
by wind (Priest) on Feb 01, 2011 at 18:37 UTC

    You're running profileb() before you open the tie'd file. Simply move your function call to after the tie statement, or remove the need for a function completely.

    -Miller

      I did say it was painfully obvious - I've only been working on this project for 5 weeks trying to get it down to a dull roar. I'm at the point that my eyes are crossed and I'm dreaming code.

      The script posted here was just a test - the real script will be outputting a 140 mg file.

        This is the very problem one avoids by not using global vars to pass data to functions.
Re: Tie::File is driving me crazy
by toolic (Chancellor) on Feb 01, 2011 at 18:42 UTC
Re: Tie::File is driving me crazy
by ikegami (Pope) on Feb 01, 2011 at 19:04 UTC

    Does a bit less work:

    sub popfileb { #set first row; substr $aox[0], 0, 11, 'n' x 11; foreach my $yb ( 1 .. 10 ) { foreach my $xb ( 0 .. 10 ) { #set increasing rows; substr $aox[$yb], $xb, 1, 'a'; } $aod[$yb] = $aox[$yb] . "\n"; } }

    I disapprove of doing IO in a function that has nothing to do with IO, though. «$aod[$yb] = $aox[$yb] . "\n";» shouldn't be in that function.

      The actual working script doesn't have that in it. THIS is the real popfileb:

      It's now working.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://885584]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2014-07-31 10:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (248 votes), past polls