SPUG: 4 Quick Questions (symlinks, anon. arrays, shell commands...)

Ken McGlothlen mcglk at serv.net
Fri Sep 17 16:20:13 CDT 1999


Ryan Erwin <ryan at erwin.org> writes:

| Question 1:
| Say i have a hash: %hash
| The current key of the hash is $_, accessed by $hash{$_}.  I have values
| qw(x y z) that I want to push into an anonymous array in the value of
| $hash{$_}.  I can't remember how!

A disclaimer before I go on:  NONE OF THIS IS TESTED CODE.

Wouldn't this just be something like

	$hash{$_} = ["x", "y", "z"];

?

Oh, wait.  You want to *push* the values into an existing array reference?

	push( @{$hash{$_}}, qw( x y z ) );

That should still work.

| Question 2:
| Say my file system contains this: ( -> is a symlink )
| /usr/bin/file1
| /bin/file2 -> ../usr/bin/file1
| /bin/file3 -> /usr/bin/file1
| /bin/file4 -> ../usr/../usr/../usr/bin/../bin/file1
| /bin/file5 -> /usr/bin/../../usr/bin/file1
| /bin/file6 -> file2
| 
| How can I resolve where these links are pointing to?  The only way that I
| have figured out is to chdir to the directory that contains the symlink i'm
| reading, strip out the path given by readlink, then chdir to the path.  Then
| I still don't know how to get the working directory without using the shell
| (`pwd`).

You may want to take a look at the Cwd module that comes with Perl 5.  (I was
recently reminded of this myself.)  It implements the getcwd() function, the
abs_path() function, and so on.  It tries to ensure portability across Unix,
VMS and Windows currently.

So you'd do something like

	use Cwd;
	$where = abs_path( readlink( "/bin/file4" ) );
	
| Question 3:
| Is there a way to find the current working directory from perl without using
| pwd in backticks?  ( my $dir = `pwd` )

	use Cwd;
	$cwd = getcwd();

| Question 4:
| Is there a way to find out what a file is dynamically linked with besides
| using ldd in backticks?  ( my $_ = `ldd $file` )

Okay, you got me there.  There's nothing that I know of in CPAN that does this,
either.  :)

Good luck with your project.

							---Ken

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    POST TO: spug-list at pm.org        PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
        Email to majordomo at pm.org: ACTION spug-list your_address





More information about the spug-list mailing list