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

Fellow Monks!

I respectfully ask for your help.
THe following code(below) works perfectly. The goal is to add the numeric content in the variable $count as a line to the beginning of a file.
the good code:
#!/usr/bin/perl -w use strict; my $count=8; use Tie::File; tie my @lines, 'Tie::File', 'T:\temp.txt' or die 'tie failed'; unshift @lines, "$count"; untie @lines;
Now.... I want to put the filename in a variable, $file, so that I can change the name through the variable if necessary. I now have this code:
#!/usr/bin/perl -w use strict; my $file="T:/temp/temp.txt"; my $count=8; use Tie::File; tie my @lines, 'Tie::File', "$file" or die 'tie failed'; unshift @lines, "$count"; untie @lines;
Which unfortunately doesn't work. I get the following error:
tie failed at P:\test405.pl line 9
And.... line 9 just happens to be where I am doing the file tie to the array. I did indeed verify that the file still exists and the permissions are OK. I checked in the CPAN documentation for this module, and from what I saw in it this line:
tie my @lines, 'Tie::File', "$file" or die 'tie failed';
.... should indeed work. What finite subtlety am I missing here? I am figuring that the problem must be right under my nose and I am simply missing it.
Your help and wisdom are greatly appreciated!
Thanks in advance!

Replies are listed 'Best First'.
Re: Tie::File Help
by deibyz (Hermit) on Apr 27, 2005 at 13:30 UTC
    Try printing also $! to see why the tie failed. More information in perlvar
      *sigh* ..sometimes the obvious stands before us screaming and jumping up+down... yet we do not see.
      Your suggestion revealed to me the exact problem.
      Thanks for your help!!
        It happens to me every day. That's why I always die with $! at the end of the die message.
Re: Tie::File Help
by polettix (Vicar) on Apr 27, 2005 at 13:11 UTC
    Slashes? I see you use backslashes in the good code, and forward slashes in the bad.

    Flavio (perl -e 'print(scalar(reverse("\nti.xittelop\@oivalf")))')

    Don't fool yourself.
      actually It tried it this way (line4):
      my $file='T:\temp\temp.txt';
      and this way:
      my $file="T:\\temp\\temp.txt";
      (with the double quote the initial backslash has to be escaped lest a \t tab be interpreted)
      anyway.... both brought down the same error. also tried
      tie my @lines, 'Tie::File', $file or die 'tie failed';
      ...and...
      tie my @lines, 'Tie::File', "$file" or die 'tie failed';
      to no avail..... (same error)
        Another guess: in the first working example, you use file "T:\temp.txt", while in the not-working one you use "T:\temp\temp.txt". Are you sure that directory "T:\temp" exists in drive T, and you have the rights to write it?!?

        Flavio (perl -e 'print(scalar(reverse("\nti.xittelop\@oivalf")))')

        Don't fool yourself.
Re: Tie::File Help
by bofh_of_oz (Hermit) on Apr 27, 2005 at 13:49 UTC
    The funny thing is, I tried your script on a Linux box AND a Windows box, and it worked perfectly...

    My guess is your T: drive is a network drive... try to test the script on a local file first, just in case...

    --------------------------------
    An idea is not responsible for the people who believe in it...