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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Windows Filenames 2

Status
Not open for further replies.

rvBasic

Programmer
Oct 22, 2000
414
BE
This is probably trivial to most of you, but I still do not feel very confident in this area[sadeyes]

Following program tries to constitute a filename from its basic constituents:
Code:
$sa='C:\tabc\nxyz';
$sb='log.txt';
$dsn1="$sa\\$sb";
$dsn11=$sa.'\\'.$sb;
print "dsn1= $dsn1\n";
print "dsn11= $dsn11\n";

print "\n";

$sc="C:\tabc\nxyz";
$sd="log.txt";
$dsn2="$sc\\$sd";
$dsn22=$sc.'\\'.$sd;
print "dsn2= $dsn2\n";
print "dsn22= $dsn22\n";

The name parts have deliberately escape characters sequences. Judging the output, either dsn1 or dsn11 is the way to go:
Code:
STDOUT output area
dsn1= C:\tabc\nxyz\log.txt
dsn11= C:\tabc\nxyz\log.txt

dsn2= C:	abc
xyz\log.txt
dsn22= C:	abc
xyz\log.txt
Out of the two possibilities, I would rather prefer option dsn11 - as to avoid all interpolation.

In my real program however I am not in control of the filenames: they are read from a database into a perl variable (via DBI ODBC) and then split into their base/dir parts (via use File::Basename). Or they can be read by readdir into an array.

My question now is: Can I be sure that the variable that is used to read the filename behaves like a single quoted string (like $sa and $sb)? And if not what is the best way to handle this problem?

PS: Windows is the OS (and I don't like a mixture of forward- and backward slashes in the same filename)

_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
$sc=tr/\\/\//g;

should change all your \'s to /'s, is this what you're after?

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Windows supports forward-slashes in file paths, so use them instead of back-slashes.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
[blue]PaulTeg,KevinADC[/blue]: Thanks for replying. The thought occured to me of translating all slashes to forward slashes. And this should work, at least for my script.

But I do try to understand how interpolation and escape characters work together. As I wote, my filenames are stored in a database with backslashes (other programs consult the same database).

While testing, I deliberately introduced a filename (in the database record) containing a \n sequence, very much like the $a in the code. Reading that filename into a perl variable and subsequently using or even printing it did not produce any strange behaviour. No new lines or other surprises. The Perl variable behaved as if it contained a single quoted string like the top half of the code above.

But I can't find confirmation of that in the documentation I've read so far. Hence my worries [ponder]


_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Input from files/databases is treated like single-quoted strings and should produce no odd behavior with meta characters unless used in a regexp.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Yes, as Kevin says, interpolation isn't a concern for inputs from the database.

One suggestion that I would make to you is to use File::Spec along with File::Basename for your file system processing. This way you don't even have to worry about the decision to use either forward or back slashes. Let perl decide for you:

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]File::Spec::Functions[/green] [red]qw([/red][purple]catfile[/purple][red])[/red][red];[/red]

[black][b]use[/b][/black] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$path[/blue] = [red]'[/red][purple]C:\tabc\nxyz[/purple][red]'[/red][red];[/red]
[black][b]my[/b][/black] [blue]$file[/blue] = [red]'[/red][purple]log.txt[/purple][red]'[/red][red];[/red]

[black][b]my[/b][/black] [blue]$fqfn[/blue] = [maroon]catfile[/maroon][red]([/red][blue]$path[/blue], [blue]$file[/blue][red])[/red][red];[/red]

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]fqfn = [blue]$fqfn[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

[gray][i]# Outputs: fqfn = C:\tabc\nxyz\log.txt[/i][/gray]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
Core (perl 5.8.8) Modules used :
[ul]
[li]File::Spec::Functions - portably perform operations on file names[/li]
[/ul]
[/tt]

- Miller
 
[blue]KevinADC[/blue] This is reassuring! and, [blue]MillerH[/blue] I'll consider your suggestion.

_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top