Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Out of date over <> and 5.10

by hsmyers (Canon)
on Sep 07, 2010 at 22:24 UTC ( [id://859221]=perlquestion: print w/replies, xml ) Need Help??

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

I've momentarily be jerked from my safe cocoon of 5.8 into the world of 5.10 (and 64 bit at that) and was dismayed to find that
while(<>) { # do something with $_ }
no longer seems to work. I don't mind changing to an explicit open, but I have to admit that I miss the 'nix filter of previous versions. What am I doing wrong?

UPDATE: Here is the detail that I should have supplied in the first place, sorry about that! It also contains the answer to the problem in that the failure is related to redirection...

C:\>perl -v This is perl, v5.10.0 built for MSWin32-x64-multi-thread (with 5 registered patches, see perl -V for more detail) Copyright 1987-2007, Larry Wall Binary build 1004 [287188] provided by ActiveState http://www.ActiveSt +ate.com Built Sep 3 2008 12:22:07 Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5 source ki +t. Complete documentation for Perl, including FAQ lists, should be found +on this system using "man perl" or "perldoc perl". If you have access to + the Internet, point your browser at http://www.perl.org/, the Perl Home Pa +ge. C:\>type test.pl #!/usr/bin/perl # test.pl use warnings; use strict; while (<>) { print $_; } C:\>test test.pl #!/usr/bin/perl # test.pl use warnings; use strict; while (<>) { print $_; } C:\>test < test.pl C:\>
--hsm

--hsm

"Never try to teach a pig to sing...it wastes your time and it annoys the pig."

Replies are listed 'Best First'.
Re: Out of date over <> and 5.10 (redir)
by tye (Sage) on Sep 08, 2010 at 04:40 UTC

    Redirection not working for Perl scripts on Windows for some instances of Perl has been reported for a very long time. You can always fix it by specifying 'perl' in the command line (like "perl -S test.pl < test.pl") or by transforming test.pl into test.bat by using pl2bat or something similar.

    I have never seen anybody even come close to isolating what causes some installs to exhibit this problem while other installs do not.

    The obvious thing to look at is the output of "assoc .pl" and "ftype SEEABOVE", for example:

    C:\> assoc .pl .pl=perlscript C:\> ftype perlscript perlscript=perl "%1" %* C:\>

    But I believe that previous cases showed no anomalies with the above settings.

    I personally just use pl2bat and don't worry about configuring *.pl associations or adding .PL to PATHEXT. But it does mean that Ctrl-C prompts you annoyingly (but I find a lot of other benefits to it).

    - tye        

      I have never seen anybody even come close to isolating what causes some installs to exhibit this problem while other installs do not.

      Now you have! :)

      But only as a result of your prompting my memory with your post:

      C:\test>set PATHEXT=.COM;.EXE;.BAT;.CMD; C:\test>junk.pl junk.pl 'print "'$_'" while <>; ' C:\test>junk junk.pl C:\test>set PATHEXT=.pl;.COM;.EXE;.BAT;.CMD; C:\test>junk junk.pl 'print "'$_'" while <>; '

      Personally, I dislike pl2bat with a vengeance. I've tried it several time over the years and always abandoned it almost immediately for one reason or another. I haven't tried to use it now for several years and can't remember all of the reasons.

      But the Terminate batch job (Y/N)? prompting is only one.

      Another is it screws up syntax highlighting. I could associate .bat with the .pl profile, but then that screws up highlighting in .bats. Another is forgetting that I pl2bat'd some script and the spending a few minutes--or on one particularly memorable occasion a few days--tearing my hair out before working out why none of the changes I was making to the .pl file were affecting the output from the script :)

      Dumb! But I've done it several times. There are others. Perl::Tidy etc. Yes. It is just a matter of discipline. And only pl2batting once you've finished developing which is reasonably easy to do. But it's when you come back to something a few months or years later that it goes tits up.

      Once you have PATHEXT/ASSOC/FTYPE set up correctly, everything "Just works". And setting it up is a one-time deal. I also like the multiple side-by-side perl installs that it affords through the simple mechanism of using different extensions .pl for 5.latest, .pl8 for 5.8 etc.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        One thing you might slightly prefer over using pl2bat might be the following cmd file:

        @echo off setlocal set cmd=%~f0 set cmd=%cmd:~0,-4%.pl perl -w %CMD% %*

        This will (try to) invoke a Perl script (ending in .pl) in the same directory and with the same name as the .cmd file this code lives in. You could copy this file besides each Perl script you want to run from the command prompt.

        But as you have set up the file associations appropriately already, likely this won't be an issue for you.

        C:\test>junk junk.pl C:\test>

        That's not the behaviour the OP sees. You should be getting an error for that.

        >junk junk.pl 'junk' is not recognized as an internal or external command, operable program or batch file. >junk < junk.pl 'junk' is not recognized as an internal or external command, operable program or batch file. >set PATHEXT=.pl >junk junk.pl 'print "'$_'" while <>; '

        I suspect you messed up your test.

        Another is it screws up syntax highlighting.

        Ah, yes, the piles of useful tools that I've seen discarded because they get in the way of the syntax coloring crutch continues to grow. I strongly prefer code that is easy to read without the need for colors, especially because I read code in lots of ways (in diff output, in patches, on printed pages, several different ways via revision control systems, etc.) that just don't support syntax coloring. I much prefer to be able to select my tools rather than have one fragile tool eliminating lots of other tools for me. But I realize that this places me in a small minority.

        I also don't use Perl::Tidy (for similar reasons: it is rather fragile and I've seen people discard tools because they confuse the poor thing, just as you have, but also because I just don't find much benefit to it). And so using pl2bat doesn't actually require much of any "discipline" for me.

        Once you have PATHEXT/ASSOC/FTYPE set up correctly, everything "Just works".

        Not for everybody, at least not if you want to use piping or redirection. Or did you already forget the reason for this very thread?

        And setting it up is a one-time deal.

        Ah, the luxury of only ever running your code on a single computer. I'm now at a job where I have the luxury of not having to deal with a large and ever-growing list of Windows-based computers (the list of Windows-based computers I must deal with is quite small and grows only very slowly).

        I also like the multiple side-by-side perl installs that it affords through the simple mechanism of using different extensions .pl for 5.latest, .pl8 for 5.8 etc.

        s/simple mechanism/awkward hack/, IMHO. It must take some discipline to come up with a new extension for each new install of Perl and then to remember to update your syntax coloring configuration so that it knows about this growing list of custom extensions. ;) Using something like pl2bat means you pick which Perl to use by actually specifying the perl to use in the script, much like a #! line under Unix. pl2bat doesn't prevent using side-by-side Perl installs. It just affords them in a more straight-forward way.

        I also like being able to use bl2bat-wrapped Perl scripts to change the environment or working directory of the calling command prompt (I've posted quite a few of those here). It is also useful for making scripts that can be used by my co-workers even if they don't have Perl installed on their Windows box (because my install of Perl is available via a network share).

        By no means am I trying to convince you (or anyone, really) to use pl2bat. I'm just noting why, for me, the down side is so small (just the "Terminate batch job" prompting) and the up side well worth it. :)

        - tye        

Re: Out of date over <> and 5.10
by roboticus (Chancellor) on Sep 07, 2010 at 22:39 UTC

    hsmyers:

    Perhaps you should reinstall it, as it seems broken. It works fine here:

    Roboticus@Roboticus-PC ~ $ perl t.pl t.pl #!/usr/bin/perl use strict; use warnings; while (<>) { print; } Roboticus@Roboticus-PC ~ $ perl --version This is perl, v5.10.1 (*) built for i686-cygwin-thread-multi-64int (with 12 registered patches, see perl -V for more detail)

    ...roboticus

Re: Out of date over <> and 5.10
by GrandFather (Saint) on Sep 07, 2010 at 22:46 UTC

    Works fine for me with a 32 bit build of Perl 5.10.1 on 64 bit Windows 7:

    use strict; use warnings; use 5.010; open my $outFile, '>', 'delme1.txt'; print $outFile join ("\n", map "File 1 line $_", 1 .. 3), "\n"; close $outFile; open $outFile, '>', 'delme2.txt'; print $outFile join ("\n", map "File 2 line $_", 1 .. 3), "\n"; close $outFile; local @ARGV = ('delme1.txt', 'delme2.txt'); while (<>) { print $_; }

    Prints:

    File 1 line 1 File 1 line 2 File 1 line 3 File 2 line 1 File 2 line 2 File 2 line 3
    True laziness is hard work
Re: Out of date over <> and 5.10 ("doesn't work")
by tye (Sage) on Sep 07, 2010 at 23:32 UTC
    What am I doing wrong?

    Giving a horrid problem description? "Doesn't seem to work" is the classic near-useless problem description. What specifically are you seeing with what exact code that makes you think it isn't working? I'd go into more detail but after 470 write-ups here, I suspect you can handle it now that it has been pointed out to you.

    - tye        

      Yes Tye! I stand abashed--- I'll edit in a bit, sorry! --hsm

      --hsm

      "Never try to teach a pig to sing...it wastes your time and it annoys the pig."
Re: Out of date over <> and 5.10
by ikegami (Patriarch) on Sep 08, 2010 at 02:16 UTC

    This isn't related to Perl. Windows had problem with running non-executables with redirection. I thought that was a thing of the past, but I guess not. I've had no problem with WinXP and Win7 with a wide variety of ActiveState builds, but my system isn't setup in a way that allows omitting the extension. Maybe the problem lies with that feature. You might have better luck specifying the extension. If that doesn't help, you'll have to specify the executable. You also have the option of using pl2bat.

      Windows had problem with running non-executables with redirection. I thought that was a thing of the past, but I guess not.

      Huh?

      c:\test>type junk.pl print while <>; c:\test>perl junk.pl junk.pl print while <>; c:\test>perl junk.pl < junk.pl print while <>; c:\test>junk.pl junk.pl print while <>; c:\test>junk.pl < junk.pl print while <>; c:\test>junk junk.pl print while <>; c:\test>junk < junk.pl print while <>; c:\test>del junk.pl c:\test>copy con junk.pl print "'$_'" while <>; ^Z 1 file(s) copied. c:\test>type junk.pl | perl junk.pl 'print "'$_'" while <>; ' c:\test>type junk.pl | junk.pl 'print "'$_'" while <>; ' c:\test>type junk.pl | junk 'print "'$_'" while <>; '

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Am I missing something? You say "Huh?", but you seem to not suffer from the problem either.
Re: Out of date over <> and 5.10
by dasgar (Priest) on Sep 07, 2010 at 22:49 UTC

    I tried the code below on my systems (Windows 7 64-bit, ActiveState Perl 5.12.1 32-bit) without any issues.

    use strict; while (<>) { print "You typed:\n$_\n"; }

    Output:

    test You typed: test Hello You typed: Hello

    I realize that's not the same version of Perl, but just thought I'd share another data point with you.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2024-04-25 06:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found