Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Opening filenames with special characters

by Zadeh (Beadle)
on Mar 12, 2007 at 17:25 UTC ( [id://604400]=perlquestion: print w/replies, xml ) Need Help??

Zadeh has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I can't seem to figure out a way to open filenames with the '*' character in it. I have a general subroutine that looks like so:
sub blah { my $file = shift; ... my $retval = open(FHANDLE, $file); ... }
But that always fails. I've also tried using e.g.:
my $retval = sysopen(FHANDLE, $file, O_RDONLY);
But that also fails. Is there any way to do this?

(And please don't ask me why I'd want to do this, or if I can just not do it, etc. Yes I really do, and I don't know why some users/software creates such horridly named files, but I have to process them nonetheless. :))

Replies are listed 'Best First'.
Re: Opening filenames with special characters
by kyle (Abbot) on Mar 12, 2007 at 17:31 UTC

    I think your code should work as-is. Maybe the problem is in some other part of it? If your open fails, be sure to print $! to find out why.

    open my $fhandle, '<', 'foo*bar' or die "Can't read foo*bar: $!";
      Thanks,
      It turns out the problem was specific to windows and the MVFS filesystem provided by ClearCase from IBM/Rational. The same filesystem viewed from *nix coincidentally doesn't have the problem when using sysopen()...ugh

      It also turned out that any other piece of software couldn't open these same kind of files with a '*' in it in windows/MVFS, and the error message from $! was something like "Invalid filename". Copying the file to a different filesystem like NTFS worked fine.

      Hope this obscure problem happens to someone out there at some point..
        aye, \/:*?"<>| are not valid characters in Windows file names.
Re: Opening filenames with special characters
by Joost (Canon) on Mar 12, 2007 at 17:37 UTC
Re: Opening filenames with special characters
by shandor (Monk) on Mar 12, 2007 at 18:02 UTC

    If you're using a shell command (in system() or ``'s) to get the file name, don't forget to chomp the output. You might have a \n on there. Another thing I do when working with strange characters is output the variable and run it through od -c (on a Unix system) to see if some hidden characters are hiding from me.

Re: Opening filenames with special characters
by whereiskurt (Friar) on Mar 12, 2007 at 19:49 UTC
    I've started to using this pragma at the top of my scripts:
    use Fatal qw( open close );
    Basically the functions listed will now throw exceptions when they fail (use eval {}), instead of setting failure values and returning false. Fatal is part of the standard Perl modules. I learned this in 'Perl Best Practices' -- best book ever. Damian Conway, if you ever read this, I love you in a purely technosexual way. :) KPH
      use 6; return all('thanks', 'appreciation') but ETOOMUCHINFO;

      ;-)

      If you're checking for failure on close() because you write to stuff, you ought to be checking for failure on write as well. Typically that might be print() or die.

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re: Opening filenames with special characters
by ferreira (Chaplain) on Mar 13, 2007 at 02:31 UTC
    Hi, I can't seem to figure out a way to open filenames with the '*' character in it.

    As a very immediate way to figure that out, I would suggest reading the open man page. It will tell you how magic 2-arg open is and how 3-arg open is just what you want.

    open FHANDLE, '<', $file # handle anything your OS can or die "can't open $file: $!";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://604400]
Approved by kyle
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found