Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Opening filenames with special characters

by Zadeh (Beadle)
on Mar 12, 2007 at 17:25 UTC ( #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: $!";
      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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://604400]
Approved by kyle
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2017-08-20 02:31 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (313 votes). Check out past polls.