Perl Tutorial
Fundamentals
Input and Output
Control Flow
Arrays and Lists
Hash
Scalars
Strings
Object Oriented Programming in Perl
Subroutines
Regular Expressions
File Handling
Context Sensitivity
CGI Programming
Misc
Perl provides a straightforward mechanism to work with files, including reading from and writing to them. Here's a simple tutorial on writing to a file in Perl.
Before you can write to a file, you must open it using the open
function. The basic syntax is:
open(FILEHANDLE, MODE, FILENAME) or die "Error message";
>
: Write to a file (creates or truncates).>>
: Append to a file.Once the file is open, you can write to it using the print
function.
print FILEHANDLE "Text to write to the file";
It's a good practice to close a file after you're done with it using the close
function.
close(FILEHANDLE);
Here's a simple example that demonstrates opening a file, writing to it, and then closing it.
#!/usr/bin/perl use strict; use warnings; # File name my $filename = 'output.txt'; # Open the file for writing open(my $fh, '>', $filename) or die "Could not open file '$filename' $!"; # Write to the file print $fh "Hello, World!\n"; # Close the file close($fh); print "Written to $filename successfully!";
In this example:
output.txt
.die
and print an error message.print
function to write "Hello, World!" to the file.When working with files, always check for possible errors and handle them gracefully. The or die
construct after the open
function is a simple way to ensure your script stops if there's an error opening the file.
How to open and write to a file in Perl:
my $filename = "output.txt"; open my $filehandle, '>', $filename or die "Cannot open file: $!"; print $filehandle "Hello, Perl!\n"; close $filehandle;
Appending data to a file in Perl:
my $filename = "output.txt"; open my $filehandle, '>>', $filename or die "Cannot open file: $!"; print $filehandle "Appending data\n"; close $filehandle;
Perl write to file example:
my $filename = "output.txt"; open my $filehandle, '>', $filename or die "Cannot open file: $!"; print $filehandle "Writing to file\n"; close $filehandle;
File I/O in Perl scripting:
my $filename = "input.txt"; open my $filehandle, '<', $filename or die "Cannot open file: $!"; my $content = <$filehandle>; close $filehandle;
Perl open file for writing:
open
function to open a file for writing.my $filename = "output.txt"; open my $filehandle, '>', $filename or die "Cannot open file: $!";
Writing multiple lines to a file in Perl:
my $filename = "output.txt"; open my $filehandle, '>', $filename or die "Cannot open file: $!"; print $filehandle "Line 1\nLine 2\nLine 3\n"; close $filehandle;
File handling functions in Perl:
open
, print
, close
for file handling.my $filename = "data.txt"; open my $filehandle, '>', $filename or die "Cannot open file: $!"; print $filehandle "Data goes here\n"; close $filehandle;
Perl file permissions when writing:
my $filename = "output.txt"; open my $filehandle, '>', $filename or die "Cannot open file: $!"; print $filehandle "Writing with permissions\n"; close $filehandle;
Error handling in Perl file writing:
my $filename = "output.txt"; open my $filehandle, '>', $filename or die "Cannot open file: $!"; print $filehandle "Data\n" or die "Cannot write to file: $!"; close $filehandle;