Re: working example File::Taill

From: Richard Morse (remorse_at_partners.org)
Date: 03/19/04


Date: Fri, 19 Mar 2004 15:22:14 -0500

In article <2gI6c.41274$HQ6.2880664@phobos.telenet-ops.be>,
 "Jim roos" <Jimroos@uunet.be> wrote:

> Hi there,
>
> As a newbie to perl i have problems to get the following script to run
> properly.
> I get the message
> Global symbol "$file" requires explicit package name at test.pl line 09
> Global symbol "$line" requires explicit package name at test.pl line 10
> Can someone help me out please...
>
>
> #!/usr/bin/perl
>
> use MIME::Parser;
> use strict;
> use File::Tail;
>
>
>
> $file=File::Tail->new("/home/noc/procmail");
> while (defined($line=$file->read)) {
> print "$line";
> }

Thank you for using strict!

Try this:

#!/usr/bin/perl
use warnings;
use strict;
use MIME::Parser;
use File::Tail;

my $file = File::Tail->new("/home/noc/procmail");
while(my $line = $file->read) {
   print "$line";
}

The error messages are because you need to explicitly tell Perl what the
scoping of your variables is: I've shown you how to create a lexically
scoped variable, using the 'my' in front of the variable declaration.
You could also use 'local', or 'our', although they have different
meanings, which you should look up in your reference.

Also, the 'defined()' test is superfluous (I think?) -- the '=' operator
returns the value assigned, and undef is false...

HTH,
Ricky



Relevant Pages

  • Re: tricky list comparison problem
    ... > I'm having a bit of an issue with a script I'm working on. ... > - 69078878" as available sectors. ... > use strict(); ... You are telling perl to load the 'strict' module and then you are telling the ...
    (perl.beginners)
  • Re: Reading STDIN seems to be breaking my script
    ... CGI Perl script. ... As soon as the code is executed, the rest of the script doesn't ... It wouldn't run under strict. ... The commented-out code would very probably not work as intended ...
    (comp.lang.perl.misc)
  • Re: A newbie question - line number inside the script
    ... I'm a newbie to Perl, ... use strict; ... use warnings; ...
    (perl.beginners)
  • Re: sort stdin and print
    ... The script gets ip's. ... > I would like to sort it and and eliminate duplicates count the ... > in perl. ... > use strict; ...
    (perl.beginners)
  • Re: Please explain following script for me. Thanks in advance.
    ... Please quote some context of the posting you're answering ... But you don't program in Perl -- you just want to use ... You simply post your whole script here -- just saying that it doesn't ... Could you explain why there is a #-sign in front of "use strict"? ...
    (comp.lang.perl.misc)