Sponsored Content
Full Discussion: user input in perl?
Top Forums Shell Programming and Scripting user input in perl? Post 302595275 by pseudocoder on Thursday 2nd of February 2012 02:07:29 PM
Old 02-02-2012
Corona688's answer is pretty right, but don't forget to chomp!
chomp removes the newline character from the end of line.
Code:
chomp($LINE);

the line input operator includes the newline at the
end of the input. There are lots of ways to remove that newline, but
the best way is to use the "chomp" function

source: [Courses] [Perl] Part 3: User Input and "chomp"
This User Gave Thanks to pseudocoder For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

geting user input from php and using perl for execution

I am using festival speech synthesis system and I would like to allow user input in a browser. This will be taken by a php page which is then supposed to pass the input text to a perl script. The perl script should pass this text to the festival engine by executing a unix command. this in turn... (2 Replies)
Discussion started by: wairimus
2 Replies

2. Shell Programming and Scripting

Executing program with Perl script but needs user input

Hello, I'm running a perl script to execute a program through my Unix command line. The program requires a user input but I want to automatically have perl input the string. Is there a way to do this? Thanks (1 Reply)
Discussion started by: leonard2352
1 Replies

3. Shell Programming and Scripting

Accepting user input and arguments in PERL

Hi All, Can we pass arguments while calling the perl script and as well as ask user input during execution of the script? My program is as below: I am passing arg1 and arg2 as argements to test.pl ]./test.pl arg1 arg2 Inside the test.pl I have : print "Do you want a name ? (y/n) : ";... (2 Replies)
Discussion started by: jisha
2 Replies

4. Shell Programming and Scripting

User input - Perl need Help

If I want all user input to start with " : " if not display error or what I asking is how to do if statement that control a first letter of string that we want to start with. and not worry about the rest Thank (1 Reply)
Discussion started by: guidely
1 Replies

5. Shell Programming and Scripting

List all file that match user input perl

Hi, I want to list all file that match user input ( specified shell wildcard) but when I compile it dont list me #!/usr/bin/perl -w print "Enter Advance Search Function: "; chomp ($func = <STDIN>); my @files = glob("$func"); foreach my $file (@files) { print "$file\n";... (1 Reply)
Discussion started by: guidely
1 Replies

6. Shell Programming and Scripting

every time user input create array perl

Hi, How to create array every time user input and store user input and display all array print " Enter input " my @input = split(' ', $input) chmop($input = <STDIN>; foreach ($input) { @array= @input; } print @array"\n"; (1 Reply)
Discussion started by: guidely
1 Replies

7. Shell Programming and Scripting

Validating uppercase/lowercase of user input with perl compared to unix folders

Hi, I need to copy files from a source directory to a destination directory in unix. I'm using the file::copy for the actual copy. The problem is that the source and dest directories are supplied by different users, who might type the name of the directories in various combinations of lower... (6 Replies)
Discussion started by: Furou
6 Replies

8. Shell Programming and Scripting

User input in perl code

Hello friends . I am newbie to perl scripting but still managed to write a code but i am stuck at a place where i need help . Below is the code and can someone help me in taking user input for changing the font size for a html table .Thank you in advance #!/bin/ksh echo " Enter the Directory... (4 Replies)
Discussion started by: ajayram_arya
4 Replies

9. Shell Programming and Scripting

Perl to read user input

I am creating a bash that uses perl . The below code closes before the input is entered. If I run the perl as a .pl it is fine. What am I doing wrong? Thank you :). #!/bin/bash cd 'C:\Users\cmccabe\Desktop\wget' wget -O getCSV.txt http://xxx.xx.xxx.xxx/data/getCSV.csv print... (4 Replies)
Discussion started by: cmccabe
4 Replies

10. Shell Programming and Scripting

Checking the user input in perl for characters and length

My question is basically as the title says. How can I check a user inputted string is only certain characters long (for example, 3 characters long) and how do I check a user inputted string only contains certain characters (for example, it should only contain the characters 'u', 'a', 'g', and 'c')... (4 Replies)
Discussion started by: Eric1
4 Replies
Perl6::Slurp(3pm)					User Contributed Perl Documentation					 Perl6::Slurp(3pm)

NAME
Perl6::Slurp - Implements the Perl 6 'slurp' built-in SYNOPSIS
use Perl6::Slurp; # Slurp a file by name... $file_contents = slurp 'filename'; $file_contents = slurp '<filename'; $file_contents = slurp '<', 'filename'; $file_contents = slurp '+<', 'filename'; # Slurp a file via an (already open!) handle... $file_contents = slurp *STDIN; $file_contents = slurp $filehandle; $file_contents = slurp IO::File->new('filename'); # Slurp a string... $str_contents = slurp $string; $str_contents = slurp '<', $string; # Slurp a pipe... $str_contents = slurp 'tail -20 $filename |'; $str_contents = slurp '-|', 'tail', -20, $filename; # Slurp with no source slurps from whatever $_ indicates... for (@files) { $contents .= slurp; } # ...or from the entire ARGV list, if $_ is undefined... $_ = undef; $ARGV_contents = slurp; # Specify I/O layers as part of mode... $file_contents = slurp '<:raw', $file; $file_contents = slurp '<:utf8', $file; $file_contents = slurp '<:raw :utf8', $file; # Specify I/O layers as separate options... $file_contents = slurp $file, {raw=>1}; $file_contents = slurp $file, {utf8=>1}; $file_contents = slurp $file, {raw=>1}, {utf8=>1}; $file_contents = slurp $file, [raw=>1, utf8=>1]; # Specify input record separator... $file_contents = slurp $file, {irs=>" "}; $file_contents = slurp '<', $file, {irs=>" "}; $file_contents = slurp {irs=>" "}, $file; # Input record separator can be regex... $file_contents = slurp $file, {irs=>qr/ +/}; $file_contents = slurp '<', $file, {irs=>qr/ +| {2,}}; # Specify autochomping... $file_contents = slurp $file, {chomp=>1}; $file_contents = slurp {chomp=>1}, $file; $file_contents = slurp $file, {chomp=>1, irs=>" "}; $file_contents = slurp $file, {chomp=>1, irs=>qr/ +/}; # Specify autochomping that replaces irs # with another string... $file_contents = slurp $file, {irs=>" ", chomp=>" "}; $file_contents = slurp $file, {chomp=>" "}, {irs=>qr/ +/}; # Specify autochomping that replaces # irs with a dynamically computed string... my $n = 1; $file_contents = slurp $file, {chomp=>sub{ " #line ".$n++." "}; # Slurp in a list context... @lines = slurp 'filename'; @lines = slurp $filehandle; @lines = slurp $string; @lines = slurp '<:utf8', 'filename', {irs=>"x{2020}", chomp=>" "}; DESCRIPTION
"slurp" takes: o a filename, o a filehandle, o a typeglob reference, o an IO::File object, or o a scalar reference, converts it to an input stream if necessary, and reads in the entire stream. If "slurp" fails to set up or read the stream, it throws an exception. If no data source is specified "slurp" uses the value of $_ as the source. If $_ is undefined, "slurp" uses the @ARGV list, and magically slurps the contents of all the sources listed in @ARGV. Note that the same magic is also applied if you explicitly slurp <*ARGV>, so the following three input operations: $contents = join "", <ARGV>; $contents = slurp *ARGV; $/ = undef; $contents = slurp; are identical in effect. In a scalar context "slurp" returns the stream contents as a single string. If the stream is at EOF, it returns an empty string. In a list context, it splits the contents after the appropriate input record separator and returns the resulting list of strings. You can set the input record separator ("{ irs => $your_irs_here}") for the input operation. The separator can be specified as a string or a regex. Note that an explicit input record separator has no effect in a scalar context, since "slurp" always reads in everything anyway. In a list context, changing the separator can change how the input is broken up within the list that is returned. If an input record separator is not explicitly specified, "slurp" defaults to " " (not to the current value of $/ X since Perl 6 doesn't have a $/); You can also tell "slurp" to automagically "chomp" the input as it is read in, by specifying: ("{ chomp => 1 }") Better still, you can tell "slurp" to automagically "chomp" the input and replace what it chomps with another string, by specifying: ("{ chomp => "another string" }") You can also tell "slurp" to compute the replacement string on-the-fly by specifying a subroutine as the "chomp" value: ("{ chomp => sub{...} }"). This subroutine is passed the string being chomped off, so for example you could squeeze single newlines to a single space and multiple conseqcutive newlines to a two newlines with: sub squeeze { my ($removed) = @_; if ($removed =~ tr/ / / == 1) { return " " } else { return " "; } } print slurp(*DATA, {irs=>qr/[ ]* +/, chomp=>&squeeze}), " "; Which would transform: This is the first paragraph This is the second paragraph This, the third This one is the very last to: This is the first paragraph This is the second paragraph This, the third This one is the very last Autochomping works in both scalar and list contexts. In scalar contexts every instance of the input record separator will be removed (or replaced) within the returned string. In list context, each list item returned with its terminating separator removed (or replaced). You can specify I/O layers, either using the Perl 5 notation: slurp "<:layer1 :layer2 :etc", $filename; or as an array of options: slurp $filename, [layer1=>1, layer2=>1, etc=>1]; slurp [layer1=>1, layer2=>1, etc=>1], $filename; or as individual options (each of which must be in a separate hash): slurp $filename, {layer1=>1}, {layer2=>1}, {etc=>1}; slurp {layer1=>1}, {layer2=>1}, {etc=>1}, $filename; (...which, of course, would look much cooler in Perl 6: # Perl 6 only :-( slurp $filename, :layer1 :layer2 :etc; slurp :layer1 :layer2 :etc, $filename; ) A common mistake is to put all the options together in one hash: slurp $filename, {layer1=>1, layer2=>1, etc=>1}; This is almost always a disaster, since the order of I/O layers is usually critical, and placing them all in one hash effectively randomizes that order. Use an array instead: slurp $filename, [layer1=>1, layer2=>1, etc=>1]; WARNING
The syntax and semantics of Perl 6 is still being finalized and consequently is at any time subject to change. That means the same caveat applies to this module. DEPENDENCIES
Requires: Perl 5.8.0, Perl6::Export AUTHOR
Damian Conway (damian@conway.org) COPYRIGHT
Copyright (c) 2003-2012, Damian Conway. All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself. perl v5.14.2 2012-06-14 Perl6::Slurp(3pm)
All times are GMT -4. The time now is 11:14 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy