Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Bareword "FILE" not allowed while "strict subs"

by silent11 (Vicar)
on Oct 08, 2004 at 19:57 UTC ( #397758=perlquestion: print w/ replies, xml ) Need Help??
silent11 has asked for the wisdom of the Perl Monks concerning the following question:

The following code:
use strict; use CGI::Pretty qw(:standard *table); use CGI::Carp 'fatalsToBrowser'; ..... sub slurp { open(FILE,"<params.inc") || die $! ; restore_parameters(FILE); close(FILE); }
produces the following error under "use strict":
Bareword "FILE" not allowed while "strict subs" in use at D:/SITES/xyz123/cgi-bin/rows.cgi line 48.
Any idea why this is happening? restore_parameters() is coming from the CGI module and I'm using it just as documented. i think... Everything works fine when I remove "use strict" but I just can't get myself to do that.


-silent11

Comment on Bareword "FILE" not allowed while "strict subs"
Select or Download Code
Re: Bareword "FILE" not allowed while "strict subs"
by ikegami (Pope) on Oct 08, 2004 at 20:01 UTC

    Try restore_parameters(*FILE);

    Update: Adding local *FILE; as the first line of the function will protect you from accidently clobbering any other symbols, and will make sure the file gets closed at the end of the block.

Re: Bareword "FILE" not allowed while "strict subs"
by BUU (Prior) on Oct 08, 2004 at 20:07 UTC
    Better yet, assuming you are dealing with anything vaugely resembeling a modern perl, use a lexical variable as your filehandle:
    sub slurp { open(my $FILE,"<params.inc") || die $! ; restore_parameters($FILE); close($FILE); }
Re: Bareword "FILE" not allowed while "strict subs"
by bpphillips (Friar) on Oct 08, 2004 at 20:07 UTC
    I find it much safer to rely on object wrappers provided by perl (all the IO:: modules). You never have to deal with GLOBs or doing a local *FILE within a subroutine or anything. Just do:
    sub slurp { my $file = IO::File->new("params.inc","<") or die $!; restore_parameters($file); $file->close(); }
    I haven't actually used the restore_parameters call you're referring to but I'm guessing from the documentation that it would work just fine...
Re: Bareword "FILE" not allowed while "strict subs"
by TedPride (Priest) on Oct 08, 2004 at 21:18 UTC
    I prefer lexicals myself too. ++
Re: Bareword "FILE" not allowed while "strict subs"
by shenme (Priest) on Oct 08, 2004 at 21:47 UTC
    Any idea why this is happening?
    Amazing, no one answered the question.

    In the documentation you referenced they used FILEHANDLE to mean "generic file handle". In your code you did understand correctly that FILE was your file handle . . . but how to let Perl know that?

    The Perl functions like open and close expect to get a file handle as the first argument. So they automatically convert the use of a bare 'FILE' to be the use of the file handle value associated with that name. Not quite magic, but just the obvious meaning.

    But when you used 'FILE' with just any old subroutine name, like restore_parameters() from the CGI.pm module, Perl didn't know what to make of it. You could have 'meant' the scalar value, the array, the hash, ... or any of the things that might have been associated with that bare name. (Think of $FILE, @FILE, %FILE, etc.)

    The use of *FILE (a 'typeglob') as ikegami suggested more or less says give the whole smash to the subroutine so that it can figure out which value/meaning to use.

    The use of file handle values in specific variables as BUU and bpphillips demonstrated is the most explicit way for everyone to know what is meant, for _all_ uses.

      The use of file handle values in specific variables as BUU and bpphillips demonstrated is the most explicit way for everyone to know what is meant, for _all_ uses.

      I am not necessarily recommending its use here, but you seem to be overlooking the *foo{THING} syntax. The filehandle part of a typeglob can be gotten to directly by saying *FH{IO}. Here's a simple demonstration:

      $ perl -le 'open TODO, "TODO"; print *TODO{IO}' IO::Handle=IO(0x81637f0)
Re: Bareword "FILE" not allowed while "strict subs"
by Anonymous Monk on Oct 11, 2007 at 12:26 UTC
    please try: sub slurp { open("FILE","<params.inc") || die $! ; restore_parameters(FILE); close("FILE"); } Cheers, Ric
Re: Bareword "FILE" not allowed while "strict subs"
by Anonymous Monk on Oct 11, 2007 at 12:30 UTC
    sub slurp { open("FILE","<params.inc") || die $! ; restore_parameters("FILE"); close("FILE"); }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://397758]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2014-12-27 21:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls