tags:

views:

353

answers:

3

I have been using this script of mine FOREVER and I have always been using "~/" to expand my home directory. I get into work today and it stopped working:

#if ( $output   eq "" ) { $output   = "~/tmp/find_$strings[0].rslt" } # BROKEN
if ( $output   eq "" ) { $output   = "$ENV{HOME}/tmp/find_$strings[0].rslt" } #WORKS
 ...
open OUT_FILE, ">$output" or die "cant open $output : $!";

Any ideas about why this would suddenly stop worrking?

Error looks like:

cant open stephen/tmp/find_coverp.rslt : No such file or directory at /user/stephen/bin/find.pl line 137.
A: 

~ is expanded by the shell. Perl has no idea about it. So, it would work only within a shell script.

Alan Haggai Alavi
As was stated in the question, it has been working forever and just suddenly stopped working today...
stephenmm
+2  A: 

The tilde expansion is not done by perl, it is done by the shell.

You should instead use:

 use File::Spec::Functions qw( catfile );
 ...
 my $fn = catfile $ENV{HOME}, 'tmp', "find_$strings[0].rslt";
 ...
 open my $out, '>', $fn or die "Cannot open '$fn': $!";
Sinan Ünür
These are fine answers except that it was working. So why did it work at one point and what could have changed to make it stop working. Just to prove they are the same here is a diff of snapshot from a week ago: > cp .snapshot/weekly.1/find.pl ~/tmp > diff ~/tmp/find.pl ~/bin/find.pl 96c96 < if ( $output eq "" ) { $output = "/mrvl/anhomes/$ENV{'USER'}/tmp/find_$strings[0].rslt" } --- > if ( $output eq "" ) { $output = "$ENV{HOME}/tmp/find_$strings[0].rslt" } #WORKS
stephenmm
Did you read what you posted? I do not see a ~ in either of those strings.
Sinan Ünür
Strange, I see the tilde in the original post.
lexu
@lexu Look at unknown's comment above where he claims his code used to work with a ~ in the file name. He has two paths in the comment: "/mrvl/anhomes/$ENV{'USER'}/tmp/find_$strings[0].rslt" and "$ENV{HOME}/tmp/find_$strings[0].rslt" neither of which has a tilde in it.
Sinan Ünür
@sinan -- You are right. hmm... My HUGE bad... I guess maybe I never was using ~. Maybe it was that the directory structure underneath me changed. I guess I just need to make sure my little helper scripts a written better. Thanks for everyones help! stackoverflow ROCKS!
stephenmm
@stephenmm given that your questions seems to have been answered, you should choose one of the answers as *the* answer.
Sinan Ünür
+6  A: 

As stated by prior answer, "~" (tilde) is expanded by shell, not perl. Most likely, it was working due to existence of a directory "~" in your current directory, which eventually got removed, leading to the bug surfacing:

To illustrate:

  1. Tilde not working in Perl, using $ENV{HOME} works:

    $ echo MM > MM
    $ perl5.8 -e '{print `cat ~/MM`}'
    cat: cannot open ~/MM
    $ perl5.8 -e '{print `cat $ENV{HOME}/MM`}'
    MM
    
  2. Making the tilde-named directory works:

    $ mkdir \~
    $ echo MM > \~/MM
    $ ls -l \~
    -rw-rw-r--   1 DVK users          3 Jun 10 15:15 MM
    $ perl5.8 -e '{print `cat ~/MM`}'         
    MM
    
  3. Removing it restores the error, as you observed:

    $ /bin/rm -r \~
    $ ls -l \~
    ~: No such file or directory
    $ perl5.8 -e '{print `cat ~/MM`}'
    cat: cannot open ~/MM
    

This offers a plausible explanation, though I'm not 100% there can't be others.

DVK
+1 except it's "tilde"
SqlACID