LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-21-2012, 12:52 PM   #1
Daravon
Member
 
Registered: Mar 2006
Posts: 166

Rep: Reputation: 15
flushing perl STDIN buffer?


Perl is not doing what I expect.

I have a script that accepts 2 commandline arguments. Both of them will happen to be filenames, but according to my concept of standard input, the input to the script itself will just be the strings entered as arguments. If the commandline arguments happen to be filenames, the shell will not actually expand the commandline arguments to the contents of the files they are filenames for.

The first thing I do in the script is assign the values of $ARGV[0] and $ARGV[1] to variables. I don't need to access the commandline arguments after that.

Later in the script, I want to prompt the user for some input. The usual way I do this is

Code:
$userinput = <STDIN>;
However, when I do this, $userinput gets loaded with not just the first commandline argument I supplied, but since Perl is so eager to help, my variable actually gets loaded with the CONTENT of the file! Since my first argument is an image file, this is what I get. I guess there must be a carriage return embedded in the jpeg file after a few characters:

Code:
^@^PJFIF^@^A^A^A^A^A^@^@vExif^@^@II*^@^H^@^@^@
Anyway, is there some way I can flush the STDIN buffer after I assign the commandline arguments to variables, so that I can prompt for user input from the keyboard later?
 
Old 05-21-2012, 01:26 PM   #2
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,971

Rep: Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849
Hello Daravon,

please post your code completely. From what I have understood $userinput should only be the userinput, there must be something wrong in another place in your code.

Markus
 
Old 05-21-2012, 02:00 PM   #3
Daravon
Member
 
Registered: Mar 2006
Posts: 166

Original Poster
Rep: Reputation: 15
I don't have it finished, but this is what I have now

Code:
# USAGE: texpic <picture.jpg> <mydoc.tex>

# a script to jam pictures into \LaTeX documents. 
# it converts the picture to encapsulated postscript (EPS)
# format, then inserts a \figure{} element into the 
# \LaTeX document 

$printflag = 'N';

if ( !(2 == scalar(@ARGV)) ){
    die "expecting exactly 2 arguments. \n USAGE: texpic <picture.jpg> <mydoc.tex>";
}

$pic = $ARGV[0];
$file = $ARGV[1];
$pic =~ /(^.+)(\..*)/;
$picbase = $1;
#$suffix = $2;

#print "pic=$pic; \$1 = $1; \$2 = $2\n";
#print "file is $file";

qx( 
convert -scale 1000 $pic scaled_$pic
convert scaled_$pic $picbase.eps
cp $file .$file.tmp
);

print "enter caption:\n";

$caption = <>;   #this is where I need to read user input, but I get the contents of the filed specified by the first commandline arg!

I think this is just perl being normal. Normally you can do "while (<>)" or whatever and perl just starts reading in the first file supplied as an argument. That might be handy behavior normally but how do I stop it?

Last edited by Daravon; 05-21-2012 at 02:14 PM.
 
Old 05-21-2012, 03:05 PM   #4
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,971

Rep: Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849
As far as I understood what your script should do, it works here.

Markus
 
Old 05-21-2012, 05:13 PM   #5
Daravon
Member
 
Registered: Mar 2006
Posts: 166

Original Poster
Rep: Reputation: 15
It works if I use <STDIN>. If I use the <> operator by itself, it tries to read in the files.
 
Old 05-21-2012, 05:31 PM   #6
Birei
LQ Newbie
 
Registered: Nov 2010
Posts: 17

Rep: Reputation: 6
Hi Daravon,

As I understand, use splice to remove everything from your argument list, and later use <> to ask for input.
Code:
my ($pic, $file) = splice @ARGV;
...
my $caption = <>;
 
Old 05-21-2012, 05:41 PM   #7
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,971

Rep: Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849
I don't understand why the OP would not use the <STDIN> instead of <>

Markus
 
Old 05-21-2012, 05:47 PM   #8
Birei
LQ Newbie
 
Registered: Nov 2010
Posts: 17

Rep: Reputation: 6
Yes, markush. It should work using <STDIN> too.
 
Old 05-21-2012, 06:06 PM   #9
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,971

Rep: Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849
The Perldocs say that <> is only used if there are no commandline-arguments and <STDIN> in the other cases.

In my opinion the code is much more readable without <>. I would also make it a little more readable, for example:
Code:
my $usage = "expecting exactly 2 arguments. \n USAGE: texpic <picture.jpg> <mydoc.tex>" ;
die  "$usage" if @ARGV != 2 ;

my ( $pic, $file ) = @ARGV ;
$pic =~ /(^.+)(\..*)/;
my ( $picbase, $suffix ) = ( $1, $2 ) ;

# and so on
Markus

Last edited by markush; 05-21-2012 at 06:08 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
perl stdin to array casperdaghost Programming 2 05-20-2011 05:10 AM
flushing input buffer mannahazarika Programming 3 07-05-2006 11:21 AM
How to Encrypt <STDIN> in PERL 5.8 SPo2 Programming 2 01-21-2006 01:46 AM
Fedora Core 3 + Lexmark 4039 10 plus = flushing buffer nowhere Linux - Hardware 0 01-19-2005 04:25 PM
get character without enter/stdin-buffer lea Programming 3 09-07-2003 04:41 PM


All times are GMT -5. The time now is 10:39 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration