Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

How to interact with the console (input/output) running a Perl program on “Windows Power Shell ISE”?

by HelenCr (Monk)
on Feb 18, 2012 at 14:13 UTC ( [id://954754]=perlquestion: print w/replies, xml ) Need Help??

HelenCr has asked for the wisdom of the Perl Monks concerning the following question:

Hi wizards
I have a certain Perl program that I need to run on Windows 7 "Power Shell ISE" (since the regular MS Windows console does not support many Unicode fonts, and PS-ISE does, and I need to process Unicode files on Windows, using Perl).

When running a Perl program from the regular cmd console, you go, for example,
>"Perl hello.pl";
the program runs, and if you print something, let's say
print "Hello world\n";
it gets printed in the cmd box.
How can I do it in PS-ISE? In PS-ISE, when I go:
">Perl hello.pl"
it hangs.
PS-ISE says: "Running script/selection. Press Ctrl+Break to stop", but it doesn't print "Hello world".
(Next, I also need to accept input from STDIN).

Important note: it's "Power shell ISE", not: "Power Shell".
Cross-posted on StackOverflow

Replies are listed 'Best First'.
Re: How to interact with the console (input/output) running a Perl program on “Windows Power Shell ISE”?
by LonelyPilgrim (Beadle) on Feb 18, 2012 at 16:15 UTC

    I've never used PS-ISE before. In fact, just the past week I discovered it in my System32 folder and was poking at it wondering what it was.

    But I just fired it up to see what would happen. Here's what happened.

    Okay, I have a three-paned window here (I'm on a WinXP SP3/Win32 system). Top one says Untitled.ps1 (presumably an edit window for writing a shell script), middle one seems to be STDOUT shell output, bottom one is my command line.

    My little hello.pl reads:

    #!/usr/bin/perl use strict; use warnings 'all'; print "Hello, world!\n";

    And I run on my command line:

    > perl hello.pl

    And I get in STDOUT:

    PS> perl hello.pl Hello, world!

    This isn't what you get? I don't know what I'm doing differently.

    More important, why do you need Unicode console output? That's something that very few terminals support. Processing Unicode files doesn't necessarily need Unicode output to STDOUT. This PS-ISE seems like a clunky interface not meant for much console interaction; mainly meant for system automation, maybe? I would just write a regular old Perl script to process Unicode, and have it output to a file rather than to STDOUT. I've never had a problem with that. The script only outputs status messages to STDOUT to let me know that everything is working correctly.

      When I run that "hello.pl" in PS-ISE, it prints "Hello world" fine.
      But, I would like to interact with the console, using Unicode characters. When I run this program, it hangs (doesn't even print the first message, which should be straightforward):

      #!/usr/local/bin/perl # Load a conversion table from CONVTABLE to %ConvTable. Then find mat +ches in a file and convert them. use strict; use warnings; use Encode; use 5.014; use utf8; use diagnostics; use autodie; use warnings qw< FATAL utf8 >; use open qw< :std :utf8 >; use charnames qw< :full >; use feature qw< unicode_strings >; my ($i,$j,$InputFile, $OutputFile,$word,$from,$to,$linetoprint); my (@line, @lineout); my %ConvTable; # Conversion hash my ($CONVTABLE, $INPUT, $OUTPUT); print 'Conversion table: opening file: E:\My Documents\Technical\Perl +\Conversion table 1.txt'."\n"; my $sta= open ($CONVTABLE, "<:encoding(utf8)", 'E:\My Documents\Techn +ical\Perl\Conversion table 1.txt'); binmode STDOUT, ':utf8'; # output should be in UTF-8 binmode $DB::OUT, ':utf8' if $DB::OUT; # for the debugger # Load conversion hash while (<$CONVTABLE>) { chomp; print "$_\n"; @line = split; $/=','; chomp(@line); # Chomp chomps a variable if its last char equals $/ $/="\n"; # Restore $/ $ConvTable{$line[0]}=$line[1]; } # end while (<$CONVTABLE>) while ( ($i,$j) = each (%ConvTable) ) { print "$i => $j\n"; } # Open file to convert print "Input to convert: enter path\\fileName: "; $InputFile = <STDIN>; chomp($InputFile); print "\$InputFile=\"$InputFile\"\n"; $sta = open ($INPUT, "<:encoding(utf8)", $InputFile); # Open output file print "OUTPUT: enter path and fileName: "; $OutputFile = <STDIN>; chomp($OutputFile); print "\$OutputFile= \"$OutputFile\"\n"; $sta = open ($OUTPUT, ">:encoding(utf8)", ">$OutputFile"); # Iterate over lines of INPUT, convert according to %ConvTable, store +in OUTPUT while (<$INPUT>) { chomp; @line = split; foreach $word(@line) { while (($from, $to) = each(%ConvTable)) { # traverse the conv +ersion table. # check if there ar +e any matches in the word and substitue $word =~ s/$from/$to/; # substitute substrings if match } # end value in %ConvTable push(@lineout, $word); } # end foreach string in the line $linetoprint = join (", ",@lineout); print ($OUTPUT, "$linetoprint\n"); $#lineout= (-1); # empty @lineout } # end while over INPUT file

      Should I flush the STDOUT buffer? How to do it?

        Again, why do this on the command line with STDIN and STDOUT? It seems to me like more trouble than it's worth to try do what you're doing through a console interface -- which provides usually just the bare bones of character support -- particularly if you plan on doing it through something clunky like this WPS-ISE. I'm not a GUI guy at all, but it seems like it might be worthwhile to set up a simple GUI to receive the input and print the output. Have you looked at Tk? It would give you something like Windows text boxes which ought to support your Unicode fonts. You could also have a file selector dialog.

        Update: Upon fiddling with it, I now see that the Tk interface that comes with ActivePerl now, at least, is Tkx. And it has crummy documentation. But I think it might help you.

Re: How to interact with the console (input/output) running a Perl program on “Windows Power Shell ISE”?
by exilepanda (Friar) on Feb 18, 2012 at 18:42 UTC
    how about adding a $| = 1; at the first line of your code ?
      I am more or less a newbie. Please remind me what does $| = 1; do?

        "$OUTPUT_AUTOFLUSH." It forces Perl to dump STDOUT and STDERR immediately, rather than buffering them. See perlvar.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://954754]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-03-29 09:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found