Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Global symbol .... requires explicit package name at

by ZETZ (Initiate)
on Oct 19, 2009 at 17:48 UTC ( [id://802052]=perlquestion: print w/replies, xml ) Need Help??

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

UPDATE: SOLUTION PROVIDED

I have a written a working Perl script (my first one). The interpreter complains when I turn on 'use strict'. I get the following error message: "Global symbol $files requires explicit package name at...line 19...line 20...line 24".

My code:

#!/usr/bin/perl use warnings; use strict; my @files; my $downloadpath = "/home/content/c/r/e/creatingzion/html/downloads"; my $timestamp = time(); my $modified_time; #[9] in the list of file attributes. my $age_of_file; my $time_to_live = 3600; #return a list of all the mp3 files in the downloadpath opendir(DIR, $downloadpath); @files = grep(/\.mp3$/,readdir(DIR)); closedir(DIR); #end comment #compare each file's last modified time with the current time and dele +te any files older than the time to live in seconds. foreach $files (@files) { $modified_time = (stat($downloadpath."/".$files))[9]; print $modified_time; $age_of_file = $timestamp - $modified_time; if($age_of_file >= $time_to_live) {unlink($downloadpath."/".$files);} } #end comment

The only thing I can think of and only thing my research indicates is that I need to use a lexical variable. But I'm doing that already.

UPDATE: Thank you both for you help.

Replies are listed 'Best First'.
Re: Global symbol .... requires explicit package name at
by toolic (Bishop) on Oct 19, 2009 at 17:52 UTC
    foreach $files (@files)
    I would change this line to:
    foreach my $file (@files)
    That would distinguish a single file from your array of files. Of course, you must change every instance of $files to $file inside your foreach loop. See also Foreach Loops
Re: Global symbol .... requires explicit package name at
by bv (Friar) on Oct 19, 2009 at 18:06 UTC

    use diagnostics

    Global symbol "$files" requires explicit package name at 1.pl line 19. Execution of 1.pl aborted due to compilation errors (#1) (F) You've said "use strict" or "use strict vars", which indicates + that all variables must either be lexically scoped (using "my" or +"state"), declared beforehand using "our", or explicitly qualified to say which package the global variable is in (using "::").

    The variable $files isn't lexical. foreach (and for) uses $_ by default, but if you specify a variable name before the list, it's roughly equivalent to

    foreach (@files) { $files = $_; #etc...

    So to be properly strict, you need to use my. Here are some examples of odd places where you would have to use my:

    for my $i ( 0 .. 10 ) { print "$i\n" } open my $fh, '<', "/etc/passwd"; while (my $line = <>) { next if /^\s*#/ }
    print pack("A25",pack("V*",map{1919242272+$_}(34481450,-49737472,6228,0,-285028276,6979,-1380265972)))

      The variable $files isn't lexical. foreach (and for) uses $_ by default, but if you specify a variable name before the list, it's roughly equivalent to

      foreach (@files) { $files = $_; #etc...

      Actually, not even roughly! The loop variable is an alias to each element of the list processed by the for loop so indicating an equivalence using assignment is quite misleading.

      The OP should note that a for loop variable is magical and is only valid within the scope of the loop. A same named variable outside the loop, even if declared as a lexical variable using my, is a different animal to the loop variable. Consider:

      use strict; use warnings; my $var = 42; print "before: $var\n"; for $var (1 .. 10) { print "$var "; } print "\nafter: $var\n";

      Prints:

      before: 42 1 2 3 4 5 6 7 8 9 10 after: 42

      True laziness is hard work

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-20 03:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found