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

Greetings monks- With humility, my first post: if i link to an active FIFO pipe, as per the proceeding, how can I know the symlinks name via perl variable or otherwise? With gratitude, Adcoment
$ cat ./fifo.pl

#!/usr/bin/perl
open(FIFO, "> ./test.fifo")         or die $!;
print FIFO "Smoke this.\n" . $0;
close(FIFO);

$ perl fifo.pl
$ ln -s test.fifo foo

$ cat foo
Smoke this.
fifo.pl
So $0 is the script name, what $""?"" is the symlink path and/or name? Thanks!
  • Comment on Named Pipe, Linking to It and Knowing the Name of the Link

Replies are listed 'Best First'.
Re: Named Pipe, Linking to It and Knowing the Name of the Link
by roboticus (Chancellor) on Jul 27, 2010 at 03:28 UTC

    adcoment:

    That would be dependent on the OS and/or filesystem. I'm not aware of any OS/filesystem that will let you easily look up the names of all the symlinks to a file. On a *NIX box, I generally use the find command. You could scan the filesystem and look for all symlinks and check to see if they refer to the file you're interested in. But I can't think of a solution that would be fast.

    It's very much like trying to find the key for a value in a hash--the hash doesn't support lookups based on the value, so you have to scan through the hash to find the entry.

    ...roboticus

Re: Named Pipe, Linking to It and Knowing the Name of the Link
by ikegami (Patriarch) on Jul 27, 2010 at 04:01 UTC

    ./test.fifo can be resolved to foo using readlink

    You're asking for the name another process used to access a file on the system without knowing which of the process's file descriptor the process is using to access the file. In some circumstances, that would be impossible. Others, hard. You're looking for some system-dependant feature.

Re: Named Pipe, Linking to It and Knowing the Name of the Link
by cdarke (Prior) on Jul 27, 2010 at 12:11 UTC
    If you know you are going to link to the fifo, then why not do it from Perl rather than the ln(1) program? From Perl use symlink then you have the symbolic name within your perl script and you can do what you like with it.

    Like others, I can't understand why would need the symbolic link when you have the real name already.
Re: Named Pipe, Linking to It and Knowing the Name of the Link
by morgon (Priest) on Jul 27, 2010 at 06:32 UTC
    For me it is totally unclear what you want to achieve (for the link-question it is totally irrelevant whether or not test.fifo is a named pipe or just a plain file), so please elaborate a bit more...

    So $0 is the script name, what $""?"" is the symlink path

    You can find all the "special variables" in the perlvar-manpage (do a "perldoc perlvar").

Re: Named Pipe, Linking to It and Knowing the Name of the Link
by JavaFan (Canon) on Jul 27, 2010 at 07:49 UTC
    I'm confused. Your title and the description of your post mention a pipe. Your code doesn't use pipes. And you want to find out the name of a link to your output file, which is to be created only after you run your program?

    Perhaps you should make up your mind of what you want, and then we can see if it's even possible.

Re: Named Pipe, Linking to It and Knowing the Name of the Link
by adcoment (Initiate) on Jul 27, 2010 at 23:04 UTC
    Sorry for the missing code setting up the FIFO- it was late and I was confused. To better explain my goal:
    1. I execute a 3rd party program with an argument/parameter file='foo.bar'.
    2. The 3rd party program looks in /tmp/data for the file, '/tmp/data/foo.bar'.
    3. I need the contents of foo.bar to include the string 'ini=foo.bar.csv',yet the remaining 99% of the very large config file is the same.
    4. I know I can write the file /tmp/data/foo.bar before calling said 3rd program; however, I want the least amount of disk i/o and space if this is working with lots of files even on a ramdisk, so I thought of symlink as less overhead.
    5. I thought a named pipe/FIFO or socket would know the name of the file being read and output according to a special var for the "name of the sym link that points to the process that acts like a file".
    6. I was hoping a perl process could take over a directory and any file read attempted on that directory would call a process or communicate the name of file accessed and the contents for read would be dynamic based on the file name requested.
    7. The 3rd party program I have not control short of the file passing to it. Perhaps FUSE or otherwise is the answer instead of PERL?