am trying to open a file but the problem is on the third line (open...)

sub open_file {
        local $/ = undef;
        ($filevar, $filemode, $filename) = @_;
        open ($filevar, $filemode . $filename) || die ("Can't open $filename");
}

everytime i run it i get the error: Can't use string ("FILE1") as a symbol ref while "strict refs" in use.

Any help please on how to sort this out.

you need a file handle for the file.

open my $FILE, $filename or die "File opening error\n";

Only very old versions of Perl require you to name file handles as barewords such as FILE1 etc. Use lexical filehandles instead.

FILEHANDLEs without sigils are a sign of ancient Perl. This page aims to describe why this style was once common, what's wrong with it, and what you can do to fix up your legacy source code.

my $file = $filename;
open my $FILE, '<', $file or die 'cannot open file';

I assume the file your trying to open isn't named something like "filevar,filemodefilename"

thanx guys, i changed on opening files as Dandello wrote and it worked #with additional changes too to other parts of the code

I'm learning Perl and in the book I have this example:

my $input = shift;
my $output = shift;
open INPUT, $input or die "Couldn't open file $input: $!\n";
open OUTPUT, ">$output" or die "Couldn't open file $output: $!\n";

my @file = <INPUT>;
@file = sort @file;

print OUTPUT @file;

This is my INPUT file:

Well, I finally found someone to turn me upside-down
And nail my feet up where my head should be
If they had a king of fools then I could wear that crown
And you can all die laughing, because I'd wear it proudly

And this is my OUTPUT file:

1:And nail my feet up where my head should be
2:And you can all die laughing, because I'd wear it proudlyIf they had a king of fools then I could wear that crown
3:Well, I finally found someone to turn me upside-down
4:

Why there's 3 lines with text and an empty one? Line 2: should end with "proudly" and line 3 should be "If the had a king of fools ....".

Thanks in advance !

Okay, the lines in your file are being sorted in alpha order, but the last line in your original probably doesn't have a end-of-line on it - so when it's sorted, and that last line becomes line 2 and since there's no end-of-line, it runs together. (of course, putting that last end-of-line marker in will make your file 5 lines long.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.