Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: STDIN help for a new Perl coder

by linuxer (Deacon)
on Jul 15, 2009 at 16:59 UTC ( #780400=note: print w/ replies, xml ) Need Help??


in reply to STDIN help for a new Perl coder

Welcome here and to Perl.

If you read User input like that, you must know, that the linebreak will also be stored in your variable. So you must remove it; see chomp for details.

chomp( my $dir = <STDIN> );

Each statement should be completed with a ';', your chdir misses one.

You should check if chdir was successful and do something if it wasn't ($! contains the system's error message).

chdir( $dir ) or die "$dir: $!\n";

If you already changed into $dir, you must not specify it again in the glob pattern.

As you are using strict, you need to declare each new variable. You did it right with $dir, but not with @files.


Comment on Re: STDIN help for a new Perl coder
Select or Download Code
Re^2: STDIN help for a new Perl coder
by perlJourney (Initiate) on Jul 15, 2009 at 18:05 UTC

    Thank you for the advice... I forgot about declaring the @files but will do so. I also was not aware of the linebreak being stored... I did not know Perl did this. I will look into the chomp command.

    I will also add the system error message to the chdir line to give the message like you suggest.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://780400]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2014-08-02 03:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (54 votes), past polls