On Thu, Mar 17, 2011 at 11:56, Chris Stinemetz
<cstinem...@cricketcommunications.com> wrote:
> I'm trying to use file path for my file that I want to read but I am getting 
> the following error when trying to use strict.
>
> Can't use string ("C://temp//PCMD") as a symbol ref while "strict refs" in 
> use at ./DOband.pl line 10.
>
> Any help is greatly appreciated.
>
> #!/usr/bin/perl
>
> use warnings;
> use strict;
>
> my $filepath = "C://temp//PCMD";
> my $outfile  = "output.txt";
>
>
> open ("$filepath") || die "ERROR: opening $filepath\n";
> open (OUTFILE, "> $outfile") || die "ERROR: opening $outfile\n";
>
>
>
> Chris Stinemetz
>
>

The proper syntax is one of

#very old school and bad
our $FILEPATH = $filepath;
open FILEPATH;

#dangerous because we don't specify the mode,
#which means if $filepath starts with > we could accidentally
#overwrite another file
open INFILE, $filepath or die "Could not open $filepath: $!";

#better, but still using the old two argument version of open
open INFILE, "< $filepath" or die "Could not open $filepath: $!";

#pretty good, but INFILE is visible to the entire package
#and some other code may already be using it, this is
#not much a problem in small programs, but in larger ones
#it leads to obscure and hard to diagnose bugs
open INFILE, "<", $filepath or die "Could not open $filepath: $!";

#this is the best way, it is safe and $infile is scope to the enclosing block
open my $infile, "<", $filepath or die "Could not open $filepath: $!";

You can read more in

http://perldoc.perl.org/functions/open.html

or

perldoc -f open



-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to