At 2:11 AM -0500 10/18/09, Harry Putnam wrote:
I'm not sure what these errors are telling me.


I have not used File::Temp nor File::Copy very much. However, I think you are not using File::Temp in its intended way, according to the documentation. What version of File::Temp are you using? I have version 2.11 on my system under Perl 5.10.


The script is supposed to remove dups from .bash_history but not
operate on the last 12 lines... just reprinting them.

./uniqbh.pl
 Copying ./bash_history to ./bash_history-101809_020827
 Unlinking ./bash_history
 Copying /tmp/lPlN3goGnI.tmp ./bash_history
 Operation "eq": no method found,
        left argument in overloaded package File::Temp,
        right argument has no overloaded magic at
 /usr/lib/perl5/5.8.8/File/Copy.pm line 76, <BASH_HISTORY> line 18151.


Look at line 76 in the file /usr/lib/perl5/5.8.8/File/Copy.pm. If it is in routine copy or _eq, then it is either 1) trying to figure out if you have passed it a file handle or a file name, or 2) trying to figure out if you are trying to copy a file onto itself. In either case, it is getting confused by the arguments to the copy routine that you have provided.


------- 8< snip ---------- 8< snip ---------- 8<snip -------
#!/usr/bin/perl

use strict;
use warnings;

# my $BashHistory = "$ENV{'HOME'}/.bash_history";
my $BashHistory = "./bash_history";
my $data = '';
my %data;
my $ext = '.tmp';
## Creat an honest to goodness random tmp file name
require File::Temp;
use File::Temp ();

You only need one of the two preceding lines. The 'use File::Temp()' would normally be preferred.


# [HP 101709_220324  Unlink is set to 1 by default .. that
# means it will be unlinked when the object goes out of scope
# so setting it to 0 means I have to delete (unlink) it manually
my $tmpfile = new File::Temp( UNLINK => 0, SUFFIX => '.tmp' );


The File::Temp::new method returns an object which can be treated as a file name or as an open file handle. The new method opens the file, so the following open statement is unnecessary. You should use $tmpfile as a file handle. However, File::Copy may be confused by passing it a File::Temp object instead of a normal file handle.


open(TMPFILE,">$tmpfile") or die "Can't open $tmpfile: $!";


I would re-think your program design and avoid using both File::Temp and File::Copy. I probably wouldn't use either. If you already know the name of the file you want to create, then just create it with an open. Instead of opening your bash history file twice, open it once, read the file to get the number of lines, then rewind the file with the seek() method. Then, read the lines from the input history file, ignore redundant lines except for the last twelve, and write the lines to the new file. No modules required, and no file copying either.

Since bash history files are that big, you could also read the file into an array, get the number of elements in the array, and write out the array accordingly.


--
Jim Gibson
j...@gibson.org

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to