You don't need (or want) dots for concatenation inside a string. Things just interpolate:
...">$basedir/output/file.text") or die ;
Phil
| [reply] [d/l] |
fantastic, it works!
thanks phil
| [reply] |
Your code has some small inconsistencies:
my $tempname = $basedir . '/output/file.txt';
You might use string interpolation here, like: "${basedir}/output/file.txt";
open (OUTHANDLE, ">$tempname") or die;
Better use indirect filehandle and let it tell yourself *why* it died,
like: open my $oh1, '>', $tempname or die "$!";
open (OUTHANDLE, ">$basedir . /output/file.txt") or die ;
This might be a typo, but you used string interpolation ("..") *AND*
tried "string concatenation" within the interpolated string. Which won't
do what you intended.
better use something like:
my $basedir = 'results';
my $tempname = "${basedir}/output/file.txt";
open my $oh1, '>', $tempname or die "$!";
open my $oh2, '>', "${basedir}/output/file.txt" or die "$!";
instead. The ${name} thingy would allow interpolation *within* words
like "${basedir}andmore", otherwise a variable $basedirandmore would
be searched but not be found.
Regards
mwa | [reply] [d/l] |
open (OUTHANDLE, ">$basedir . /output/file.txt") or die ;
You're trying to open a file that doesn't exist.
perl -le 'print ">$basedir . /output/file.txt"'
results . /output/file.txt
There's no concatenating operator there, you clobber them all in one single string, the $basedir, the dot with spaces ( .), and /output/file.txt. Should you use $! variable, Perl would tell you what was wrong.
my $basedir = "results";
my $filename = "$basedir . /output/file.txt";
open (OUTHANDLE, ">$filename") or die "can't open ($filename): $!\n";
can't open (result . /output/file.txt): No such file or directory
You actually want,
open (OUTHANDLE, ">$basedir" . "/output/file.txt") or die $!;
but this is not good although it probably works. Use suggestions from other replies.
Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!
| [reply] [d/l] [select] |
print "2+2" doesn't print 4.
print "2.2" doesn't print 22.
You can't place Perl code in Perl string literals and except it to get executed when the string is accessed.
That's why you were directed to use interpolation (a feature of string literals) instead of the concatenation operator (a Perl operator).
| [reply] [d/l] [select] |
in addition to the problems and solutions discussed in other posts, i would like to suggest that the "tempname" variable you're concerned about having your script cluttered with is actually useful:
my $basedir = "results";
my $filename = $basedir . '/output/file.txt';
open my $outhandle, '>', $filename or die "opening $filename: $!";
...
print $outhandle $somestuff or die "writing $filename: $!";
...
close $outhandle or die "closing $filename: $!";
| [reply] [d/l] |