Search
 
SCRIPT & CODE EXAMPLE
 

PERL

perl read file

# Basic syntax:
# For reading:
open(my $in, "<", "infile.txt") or die "Can't open infile.txt: $!";
# For writing (overwrites file if it exists):
open(my $out, ">", "output.txt") or die "Can't open output.txt: $!";
# For writing (appends to end of file if it exists):
open(my $log, ">>", "my.log") or die "Can't open my.log: $!";
# Where:
# 	The first argument to open creates a named filehandle that can be
#		referred to later
#	The second argument determines how the file will be opened
#	The third argument is the file name (use $ARGV[#] if reading from the CLI)
#	The or die "" portion is what to print if there is an error opening the file
# Note, be sure to close the filehandle after you're done operating on the file:
close $in;

# You can read from an open filehandle using the "<>" operator.  In
# scalar context it reads a single line from the filehandle, and in list
# context it reads the whole file in, assigning each line to an element
# of the list:
my $line = <$in>;
my @lines = <$in>;

# You can iterate through the lines in a file one at a time with a while loop:
while (my $line = <$in>) {
  print "Found apples
" if $line =~ m/apples/;
}

# You can write to an open filehandle using the standard "print" function:
print $out @lines;
print $log $msg, "
";
Comment

read a file in perl

#!/usr/bin/perl
use warnings;
use strict;

my $filename = 'c:	emp	est.txt';

open(FH, '<', $filename) or die $!;

while(<FH>){
   print $_;
}

close(FH);
Code language: Perl (perl)
Comment

PREVIOUS NEXT
Code Example
Perl :: perl sprintf YYYYMMDD sample 
Perl :: print a variable perl 
Perl :: Perl list files and folders in a directory 
Perl :: Perl (perl 5.28.1) sample 
Perl :: what is perl poetry 
Pascal :: store input in variable in pascal 
Pascal :: pascal try except 
Pascal :: for loop pascal 
Pascal :: pascal print 
Powershell :: get-childitem supress errors silently 
Gdscript :: gdscript for loop 
Clojure :: clojure read file line by line 
Abap :: adobre forms abap tcode 
Assembly :: using shape property in flutter for circular corner 
Assembly :: include code in latex 
Assembly :: visual studio change assembly name 
Assembly :: google appscripts cell get background color 
Javascript :: jquery vslidation remove spaces from input 
Javascript :: list all node processes 
Javascript :: javascript radian to degree 
Javascript :: check react version terminal windows 
Javascript :: jquery only number allowed to 10 digit 
Javascript :: count number of checkboxes in html jquery 
Javascript :: javascript lowercase string 
Javascript :: regex date validation mm/dd/yyyy 
Javascript :: how to console.log formData 
Javascript :: js scroll to id 
Javascript :: sleep for 1 second 
Javascript :: jquery css add important 
Javascript :: dconf-editor install terminal 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =