Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: STDIN help for a new Perl coder

by linuxer (Curate)
on Jul 15, 2009 at 16:59 UTC ( [id://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.

Replies are listed 'Best First'.
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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://780400]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-26 05:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found