Skip to content
asicguru.tech@gmail.com
REGISTER LOGIN
AsicGuru Ventures
  • All Courses
  • CONTACT
  • GALLERY
  • HOME
  • Tutorials

0

Verilog Tutorial

17
  • Verilog Introduction
  • Lexical Tokens
  • Gate-Level Modelling
  • Data Types
  • Operators
  • Operands
  • Modules
  • Behavioral Modeling
  • Timing Controls
  • Procedures: Always and Initial Blocks
  • Functions
  • Tasks
  • Component Inference
  • Finite State Machines.
  • Compiler Directives
  • System Tasks and Functions
  • Test Benches

System Verilog

23
  • System Verilog Introduction
  • Data Types
  • Type Casting
  • Arrays
  • Associative Arrays
  • Dynamic Arrays
  • Queue
  • Operators
  • Procedural statements and Control flow
  • Classes
  • Inheritance
  • Encapsulation
  • This and Super operator
  • In-Line Constraints
  • Class Constraints
  • Virtual Classes
  • Parameterized Classes
  • Classes Summary
  • Singleton Classes
  • Functional Coverage Introduction
  • Covergroup
  • Tools
  • Books

Scripting

15
  • Introduction
  • Perl Tutorial
  • What is Perl
  • Perl: Syntax And Variable
  • Perl Strings
  • Perl Arrays
  • Perl Associative Arrays
  • If/While Syntax
  • File Input
  • Print Output
  • String Processing with Regular Expressions
  • Subroutines
  • Running External Programs
  • References
  • Terse Perl

Makefile

1
  • Makefile Tutorial
View Categories
  • Home
  • Tutorials
  • Scripting
  • File Input

File Input

3 min read


Variables which represent files are called “file handles”, and they are handled differently from other variables. They do not begin with any special character — they are just plain words. By convention, file handle variables are written in all upper case, like FILE_OUT or SOCK. The file handles are all in a global namespace, so you cannot allocate them locally like other variables. File handles can be passed from one routine to another like strings (detailed below).

The standard file handles STDIN, STDOUT, and STDERR are automatically opened before the program runs. Surrounding a file handle with <> is an expression that returns one line from the file including the “\n” character, so returns one line from standard input.

The <>
operator returns undef when there is no more input. The “chop” operator removes the last character from a string, so it can be used just after an input operation to remove the trailing “\n”.
The “chomp” operator is similar, but only removes the character if it is the end-of-line character.


$line = ;        ## read one line from the STDIN file handle
chomp($line);             ## remove the trailing “\n” if present
$line2 = ;      ## read one line from the FILE2 file handle
                                 ## which must be have been opened previously
Since the input operator returns undef at the end of the file, the standard pattern to read all the lines in a file is…


## read every line of a file
while ($line = ) {
    ## do something with $line
}
Open and Close
The “open” and “close” operators operate as in C to connect a file handle to a filename in the file system.
open(F1, “filename”);                 ## open “filename” for reading as file handle
F1
open(F2, “>filename”);                ## open “filename” for writing as file handle
F2
open(F3, “>>appendtome”)        ## open “appendtome” for appending
close(F1);                                   ## close a file handle
Open can also be used to establish a reading or writing connection to a separate process launched by the OS. This works best on Unix.


open(F4, “ls -l |”);                       ## open a pipe to read from an ls process
open(F5, “| mail $addr”);             ## open a pipe to write to a mail process


Passing commands to the shell to launch an OS process in this way can be very convenient, but it’s also a famous source of security problems in CGI programs. When writing a CGI, do not pass a string from the client side as a filename in a call to open().


Open returns undef on failure, so the following phrase is often to exit if a file can’t be opened. The die operator prints an error message and terminates the program.


open(FILE, $fname) || die “Could not open $fname\n”;


In this example, the logical-or operator || essentially builds an if statement, since it only
evaluates the second expression if the first if false. This construct is a little strange, but it is a common code pattern for Perl error handling. Input Variants In a scalar context the input operator reads one line at a time. In an array context, the input operator reads the entire file into memory as an array of its lines…


@a = ;         ## read the whole file in as an array of lines


This syntax can be dangerous. The following statement looks like it reads just a single line, but actually the left hand side is an array context, so it reads the whole file and then discards all but the first line….


my($line) = ;


The behavior of also depends on the special global variable $/ which is the current the end-of-line marker (usually “\n”). Setting $/ to undef causes to read the whole file into a single string.


$/ = undef;
$all = ;            ## read the whole file into one string


You can remember that $/ is the end-of-line marker because “/” is used to designate separate lines of poetry. I thought this mnemonic was silly when I first saw it, but sure enough, I now remember that $/ is the end-of-line marker.

Updated on May 4, 2025

What are your Feelings

  • Happy
  • Normal
  • Sad
Share This Article :
  • Facebook
  • X
  • LinkedIn
  • Pinterest
If/While SyntaxPrint Output

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Archives

  • May 2025

Categories

  • Slider Post
  • Uncategorized

Copyright @ Asicguru Ventures by Misbah WP | Proudly powered by WordPress