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.
-
~is expanded by the shell. Perl has no idea about it. So, it would work only within a shell script.stephenmm : As was stated in the question, it has been working forever and just suddenly stopped working today... -
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': $!";stephenmm : 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" } #WORKSSinan Ünür : Did you read what you posted? I do not see a ~ in either of those strings.lexu : Strange, I see the tilde in the original post.Sinan Ünür : @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.stephenmm : @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!Sinan Ünür : @stephenmm given that your questions seems to have been answered, you should choose one of the answers as *the* answer. -
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:
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`}' MMMaking 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`}' MMRemoving 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.
SqlACID : +1 except it's "tilde"
0 comments:
Post a Comment