Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Can a defined value be of zero length?

by graff (Chancellor)
on Apr 19, 2014 at 15:27 UTC ( #1082901=note: print w/replies, xml ) Need Help??

in reply to Can a defined value be of zero length?

In the OP script, the defined $pattern condition is entirely unnecessary; the length $pattern condition is sufficient because $pattern will be used as a string, so all that matters is that the string not be empty (and "undef" variables are equivalent to empty strings when passed to length()).

If you pipe data to the script (e.g. echo fs | your_script), you'll get the prompt string, followed by the expected output, followed by a warning (Use of unitialized value $pattern in chompů). You also get that warning if you run it "normally" (not piping to its STDIN) and hit control-D to signal end-of-input on STDIN. For "clean" operation, I'd write it like this:

#!/usr/bin/perl use strict; use warnings; my $dir = '/etc'; my $prompt = ( -t ) ? 'Enter pattern> ' : ''; print $prompt; while ( my $pattern = <STDIN>) { $pattern =~ s/\s+$//; last unless length( $pattern ); my @matched = eval { grep { /$pattern/ } glob "$dir/*"; }; print "Error: $@" if $@; print join( "\n", @matched, $prompt ); }
That relies on the "special" behavior of the file-input operator (angle brackets around a file handle) when used as the conditional portion of a "while" statement: the result is false (the loop exits) when the input operator returns "undef" (due to an end-of-file condition). See the "I/O Operators" section of the "perlop" man page (perl operators).

My version also checks whether input is coming from a terminal (as opposed to a pipe), and prints "Enter pattern > " only in that case (so that the output with pipeline usage is not contaminated by the prompting text).

I'm tempted to add $pattern =~ s/^\s+//; as well, given that I don't expect any file names in /etc to start with white space, and when reading from a keyboard, it's not unlikely to get initial as well as final spaces that are typed in by mistake.

Update: It's definitely worthwhile to read the more careful explanation in kcott's reply later in this thread (++ to that).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1082901]
[Corion]: ambrus: I wrote the handler so that it stops the timer if it's supposed to be a one-shot
[Discipulus]: are you using Prima Corion? good morning Corion and ambrus, btw
[Corion]: Discipulus: Yeah, I rediscovered it when I wanted to have a simple (cross-platform) wrapper for my OpenGL program so it has a nice window etc
[Corion]: ... and one thing leads to another, so ambrus provided the integration with AnyEvent so I can now conveniently make HTTP requests from that app too :)

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2016-12-09 09:12 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (150 votes). Check out past polls.