Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Re: Re: Re: Test for standard input

by mikfire (Deacon)
on Oct 03, 2002 at 18:05 UTC ( #202597=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Re: Test for standard input
in thread Test for standard input

Sorry, I cannot resist. Your code is ummmm doing lots of things that I do not think are what you intend.
$ARGS = @ARGV; if ( -t and $ARGS eq 0 ) { }
The more idiomatic way of saying this is
die "No input found\n" if ( -t and @ARGV == 0 );
I would also like to point out that you are testing the wrong kind of equality in your code -- you used the string equality test ( eq ) instead of the numeric test ( == ). In this case it worked, but it will someday drive you nuts trying to hunt this kind of bug down.

$names++, shift if $ARGV[0] eq "-l";
If I have figured this out correctly, you are trying to parse some command line options. I would strongly recommend using Getopt::Long, a core module that does this job very well.

I am assuming you really mean to shift the argument you just parsed out of @ARGV. Unfortunately, shift with no arguments assumes you mean @_, which is not @ARGV. Again, it likely works in this case, but I really doubt this is your intended affect.

The same holds true for the next line.

@ARGV = "-" unless @ARGV;
This code will never be executed. I am assuming it is a hold-over from your previous problems. The 'if' at the top of the program makes certain that @ARGV has to have something in it.

The assignment back to @ARGV is a little.... ugly IMHO. It took me several tries to figure out what it is doing. I would likely write this expression as

exit 0 unless ( grep { -T or $_ eq "-" } @ARGV );
but this is likely more a stylistic thing.

Oh, good use of the -w flag. It appears, though, you forgot to use strict. It will seem a pain at first, but it will really help as your programs get longer and more complex.

Mik
mikfire

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Test for standard input
by BUU (Prior) on Oct 03, 2002 at 23:57 UTC
    <pedantry>shift in a non subroutine context shifts from @ARGV.</pedantry>
      Damn. I have been programming perl for years now and I did not know this. Thanks!

      Mik
      mikfire

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://202597]
help
Chatterbox?
[ambrus]: wait. I understand no wife and children, but how do you not have to pay for a house?
[hippo]: I had a low-paid job about 20 years ago and seriously considered going down to a 3-day week. Would have worked 60% of the time for about 80% of the cash.
[Corion]: ambrus: Well, I pay rent, but don't own a house with variable/ unforeseeable costs
[hippo]: The thesholds/benefits balance at the time was nuts.
[hippo]: But that doesn't last because, you know ... politicians. :(
[Corion]: hippo: Yeah, if you still get enough money to get by and don't have expensive hobbies or other fixed costs that can work out well
[ambrus]: ah good. I pay only rent too. but that still costs significant money.
[Corion]: ambrus: Yes, but that is already budgeted for

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (13)
As of 2017-09-21 15:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (249 votes). Check out past polls.

    Notices?