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 :: print a variable perl 
Perl :: unique in perl 
Perl :: Move files to new directory 
Perl :: perl 6 
Perl :: Perl - Common Conditional Statements 
Pascal :: pascal pause 
Pascal :: delay() in Pascal 
Pascal :: comment in pascal 
Powershell :: windows 10 debloat 
Powershell :: powershell get my documents folder 
Gdscript :: godot check left mouse button 
Clojure :: how to do operations inside a list clojure 
Lisp :: common lisp ide macos 
Assembly :: google apps script format date string 
Assembly :: wstring to lpcwstr 
Assembly :: scanf prototype in c 
Assembly :: links in markdown 
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
2+6 =