Let’s see how to take input from user. It can easily be done by
Look at the following examples to realize this:
Example1:
print "What is your name?";
$name = <>;
print "How old are you?";
$age = <>;
print "Hi $name! You are $age years old.";
Example2:
print "What is your name?";
chomp($name = <>);
print "How old are you?";
chomp($age = <>);
print "Hi $name! You are $age years old.";
File I/O
Let’s go with file now. We can do it simply with Perl. Firstly, see some examples:
Make a file named “input.txt” in the same directory of the code.
input.txt contains:
5
ab
bc
cd
de
ef
Now start our first example:
use strict;
open(my $in, "<", "input.txt") or die "Can't open input.txt: $!"; open(my $out, ">", "output.txt") or die "Can't open output.txt: $!";
my $line = <$in>;
my @lines = <$in>;
for(my $i=0;$i<$line;$i++) { print $out "value is $lines[$i] here\n"; } An output file named “output.txt” will be created.
Another example with same input file:
use strict;
open(my $in, "<", "input.txt") or die "Can't open input.txt: $!"; open(my $out, ">", "output.txt") or die "Can't open output.txt: $!";
my $line = <$in>;
while (<$in>) { # assigns each line in turn to $_
print $out "Just read in this line: $_";
}
An output file named “output.txt” will be created with different output.
Hope all can understand the functionality.
When we're done with our filehandles, we should close() them (though to be honest, Perl will clean up after us if we forget):
close $in or die "$in: $!";
http://www.perl.com/pub/2000/10/begperl1.html
http://perldoc.perl.org/perlintro.html
http://www.perl.org/learn.html
http://learn.perl.org/books.html
http://learn.perl.org/tutorials/
http://www.perl.com/pub/2008/04/23/a-beginners-introduction-to-perl-510.html
http://docstore.mik.ua/orelly/perl2/prog/ch01_05.htm
http://www.tizag.com/perlT/perluserinput.php
http://www.tizag.com/perlT/perlchomp.php
http://perldoc.perl.org/functions/use.html
http://www.well.ox.ac.uk/~johnb/comp/perl/intro.html
http://www.sthomas.net/roberts-perl-tutorial.htm/ch22/use_strict_
http://docstore.mik.ua/orelly/perl/prog3/ch31_19.htm
http://debugger.perl.org/580/perldebtut.html
http://perldoc.perl.org/strict.html
. . . . . To be continued . . . . .
No comments:
Post a Comment