Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tailing ? in directory name opened by perl program. How avoid this? 1

Status
Not open for further replies.

gargamel100

Technical User
Oct 25, 2006
31
CZ
Hi all,

I am not some perl specialist and I have following code

#!/usr/bin/perl -w


print ( "Enter the name of new directory\n");

$dir=<stdin>;

mkdir ("$dir",0777);
chdir ("$dir");

print (" New directory is created and has name $dir\n");

print (" Enter the name of new file\n");

$name=<stdin>;

open ( $name, ">$name" );

print ("The fajl is opened and has name $name\n");



and it open directory and file file inside it....but in directory name there is tailing ? mark

like this:

directory_name?

Does anybody know what cause behaviour like this.
I really do not need tailing ? in directory name

Thanks in advance...
 
The most likely culprit of your errors is that you aren't chomp'ing your input from STDIN.

I also strongly suggest that you add "use strict;" to the beginning of ALL your perl scripts, and include error checking to your code, especially for file operations.

Your code is so modified below:

Code:
[gray]#!/usr/bin/perl -w[/gray]

[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]Enter the name of new directory[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$dir[/blue] = <stdin>[red];[/red]
[url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url] [blue]$dir[/blue][red];[/red]

[url=http://perldoc.perl.org/functions/mkdir.html][black][b]mkdir[/b][/black][/url][red]([/red][blue]$dir[/blue], [fuchsia]0777[/fuchsia][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Can't makedir [blue]$dir[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]
[url=http://perldoc.perl.org/functions/chdir.html][black][b]chdir[/b][/black][/url][red]([/red][blue]$dir[/blue][red])[/red] or [black][b]die[/b][/black] [red]"[/red][purple]Can't cd [blue]$dir[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]

[black][b]print[/b][/black] [red]"[/red][purple] New directory is created and has name [blue]$dir[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

[black][b]print[/b][/black] [red]"[/red][purple] Enter the name of new file[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[black][b]my[/b][/black] [blue]$name[/blue] = <stdin>[red];[/red]
[black][b]chomp[/b][/black] [blue]$name[/blue][red];[/red]

[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url][red]([/red][black][b]my[/b][/black] [blue]$fh[/blue], [red]'[/red][purple]>[/purple][red]'[/red], [blue]$name[/blue][red])[/red] or [black][b]die[/b][/black] [red]"[/red][purple]Can't open [blue]$name[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]

[black][b]print[/b][/black] [red]"[/red][purple]The fajl is opened and has name [blue]$name[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
[/tt]

- Miller
 
check the other forum where you posted too.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top