stdin - How to get file handler for reading real time input stream in perl script by ignoring the command line arguments -


i have perl script accepts command line arguments (file name) , should read input stream write file created using argument.

the problem statement while (<>), instead of waiting user input write file , tries open file. cannot use <stdin> because need call script java program

#!/usr/bin/perl use strict; use cgi; use cgi::carp qw ( fatalstobrowser ); use warnings;  #store input parameter file_name $cfgfilename; $q = cgi->new();  $cfgfilename = $q->param('filename');  #open file , write input data open (out, "> /home/$cfgfilename") or die $!;  print "content-type: text/plain\n\n"; while ( <>)  {    print out $_; } close (out);   # pass status print "success";  exit 0; 

i think you're labouring under misconception. input stream for program stdin. it's not global thing.

the "diamond operator" <> works this:

  • if filenames specified arguments, opens them , 'streams' them through stdin.
  • if no filenames specified, reads stdin.

if don't want former apply, want read stdin. doesn't matter call program - stdin, stdout , stderr program specific 'pipe' | link 1 program's stdout next program's stdin.

most exec or system calls same thing.

you should avoid calling open - it's bad style. try instead:

open ( $output, ">", "/home/$cfgfilename" ) or die $!; 

and then:

print {$output} $_ 

by doing way:

  • you lexically scope filehandles, you're not polluting namespace, , allow program detect , close when go out of scope.
  • you avoid interpolation issues of variable names entirely (e.g. unwanted "pipe" "truncate" etc.).

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -