Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

File Open Question

by IPstacks (Novice)
on Nov 14, 2001 at 06:03 UTC ( [id://125202]=perlquestion: print w/replies, xml ) Need Help??

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

Okay... I've been a perl monk for all of a few hours. I have been reading the O'Reilly press book and have made some progress. My basic question is I am trying to open a file and read in the contents and then close the file (haven't even tried to print it yet). The problem is that I get this error:

root@ns1 /root# ./script.pl
Global symbol "@data" requires explicit package name at ./script.pl line 7.
Execution of ./script.pl aborted due to compilation errors.
#!/usr/bin/perl -w use strict; my $filename = "/root/estimates.foo.com"; # declare filename open FILE, $filename or die "Can't open file: $!/n"; @data=<FILE>; close FILE;
From everything that I've read, this looks fine. Any help would be appreciated!

Replies are listed 'Best First'.
Re: File Open Question
by perigeeV (Hermit) on Nov 14, 2001 at 07:51 UTC

    root@ns1 /root# ./script.pl

    Please don't play with Perl while running as root. Please.
    And on a nameserver no less.

Re: File Open Question
by jerrygarciuh (Curate) on Nov 14, 2001 at 06:18 UTC
    You need to declare all varibles with my when using the strict pragma. So declare @data as my @data
    HTH
    jg
    _____________________________________________________
    My name is Inigo Montoya...
Re: File Open Question
by Anonymous Monk on Nov 14, 2001 at 06:14 UTC
    i doubt this is the problem.. but you should declare the variable.
      Oh, declare the @data -- I did that and it's good now. Thanks and sorry about the post.
Re: File Open Question
by grinder (Bishop) on Nov 14, 2001 at 14:18 UTC

    Note that the idiom of slurping an entire file into an array is a bad habit to get into. It's easy to do, but one day you will slurp a really big file by accident, exhaust all your RAM, start to swap, and bring your system to its knees.

    A much better way to go about reading files is as follows:

    open FILE, $filename or die "$0: cannot open $filename for input: $!\n +"; # two notes # 1. Put as much information as possible in the die string # as you can lay your hands on. # 2. That's \n, not /n, but I guess you realised that already... while( <FILE> ) { # possibly... chomp; # otherwise do what you want with $_ } close FILE;

    Creating scripts with small memory footprints is a good idea. Otherwise the other people using the system will start to equate Perl with prolifligate memory consumption, and we don't want Perl to get a bad name, right?

    --
    g r i n d e r

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-25 12:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found