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

Hi,

The problem concerns opening and reading a text file line by line (about 500 lines). I need to do the same thing across several scripts and created a package. The problem is that it doesn't print all the lines - it stops about three quarters way through. If I put the routine into the script it reads and prints the whole file no problem (indicating that the problem is not the data!)

Can anyone explain what is wrong and how to put it right? Thanks

package wslib; use strict; our @EXPORT = qw( workspace ); #place all the variables you want to ex +port by default in here (c.f. @EXPORT_OK) use Exporter; our @ISA = qw(Exporter); my $data_reg ='/root_to_file'; sub workspace { open PAGE, "$data_reg" or die "Cant open $data_reg: $!"; while (my $line = <PAGE>) { print "$line<br>"; } close PAGE; } 1;

Replies are listed 'Best First'.
Re: data leak using package
by davorg (Chancellor) on Feb 07, 2007 at 08:42 UTC

    I can't see anything obviously wrong with your code, so the problem must be somewhere else in your environment. Can you post a (short) example of a program which uses this package and exhibits the problem you're describing.

    A couple of minor code review notes:

    package wslib;

    The Perl convention is to reserve lower case package names for pragmas (like 'warnings' and 'strict'). You should rename this to something like "WSLib".

    open PAGE, "$data_reg"

    There's no need for the quotes there. In fact it is sometimes a really bad idea.

      Thanks for the general tips, I apologise for the delay in replying - I have been trying to synthesise the salient parts or the script

      In fact I have isolated what is causing the problem, but I'm not sure why or how I should change things to do what I want.
      The background is that several scripts are accessed by a single user by way of a 'work page', containing buttons for those scripts.
      When a script is finished with it uses wslib ( soon to be WSLib(!! :) ) to create a new work page, using html::template (not shown to keep the example simple.
      In this particular instance the script edits the text file $data_reg called up in sub workspace, and renames it. If I comment out the rename line, sub workspace prints out the whole file, if I leave it in it only prints three quarters of it!! How should I deal with this?

      sub edit { my $newline = join "\t", ($count_in,$current_time_in,$user_in,$passwo +rd_in,$email_in,$web_in,$agencyname_in,$business_in,$country_code_in, +$mobile_no_in,$sec_email_in,$skip); open FH, "$data_reg" or die "Can't open data_reg file for reading: $! +"; flock (FH, 1) or die "Can't lock data_reg file for reading"; my @alldata = <FH>; close FH; open FILE, '>', "$data_reg.tmp" or die "Can't open data_reg file for +reading: $!"; flock (FILE, 2) or die "Can't lock data_reg.tmp file for writing"; foreach (@alldata) { chomp; ($count,$current_time,$username,$password,$email,$web,$agencyname, +$business,$country_code,$mobile_no,$sec_email,$skip) = split /\t/; if ($user_in eq $username) { print FILE "$newline\n"; } else { print FILE "$_\n"; } } # rename ("$data_reg.tmp", "$data_reg") or die "Can't rename file: $! +"; ##only prints properly if commented out! &workspace(@exp); }
        It seems to be Dominus day today... even if it's Tuesday! Regarding this fragment:
        open FILE, '>', "$data_reg.tmp" or die "Can't open data_reg file for +reading: $ +!"; flock (FILE, 2) or die "Can't lock data_reg.tmp file for writing";
        you might benefit from these slides.

        Flavio
        perl -ple'$_=reverse' <<<ti.xittelop@oivalf

        Don't fool yourself.
Re: data leak using package
by bingos (Vicar) on Feb 07, 2007 at 08:44 UTC

    I'd suggest using In direct Filehandles for situations like this. Also by shifting off the first parameter, the function is slightly more useful, you can call the function with any filename you wish, workspace( '/root_to_file' );

    package wslib; use strict; our @EXPORT = qw( workspace ); #place all the variables you want to ex +port by default in here (c.f. @EXPORT_OK) use Exporter; our @ISA = qw(Exporter); sub workspace { my $data_reg = shift || return; open my $page, "$data_reg" or die "Cant open $data_reg: $!"; while (my $line = <$page>) { print "$line<br>"; } close $page; } 1;
      Thanks for that info - I hadn't come across it before and it looks very useful. I'm definitely a convert!!
Re: data leak using package
by polettix (Vicar) on Feb 07, 2007 at 09:20 UTC
      Nice try - but it's not that!!

      It's got to do with renaming the file immediately before using it in the lib file. Not sure why that's happening (details above).

      Thanks for your help