Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

opening files where name is a concatenation of variable

by confused_newbie (Initiate)
on Oct 10, 2007 at 15:25 UTC ( [id://643983]=perlquestion: print w/replies, xml ) Need Help??

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

Hi I need to open a file where the name is obtained by concatenating a variable and a string of text. the concatenation doesnt seem to get evaluated in the open statement. so the 2 line version below (predefining the filename) works but the second, one line version, doesnt. I don't want loads of "tempname" variables in my script. how can i get the one line version to work? thanks
$basedir="results"; my $tempname=$basedir . '/output/file.txt'; open (OUTHANDLE, ">$tempname") or die; open (OUTHANDLE, ">$basedir . /output/file.txt") or die ;

Replies are listed 'Best First'.
Re: opening files where name is a concatenation of variable
by philcrow (Priest) on Oct 10, 2007 at 15:30 UTC
    You don't need (or want) dots for concatenation inside a string. Things just interpolate:
    ...">$basedir/output/file.text") or die ;

    Phil

    The Gantry Web Framework Book is now available.
      fantastic, it works! thanks phil
Re: opening files where name is a concatenation of variable
by mwah (Hermit) on Oct 10, 2007 at 15:38 UTC
    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
Re: opening files where name is a concatenation of variable
by naikonta (Curate) on Oct 10, 2007 at 18:28 UTC
    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!

Re: opening files where name is a concatenation of variable
by ikegami (Patriarch) on Oct 10, 2007 at 16:04 UTC

    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).

Re: opening files where name is a concatenation of variable
by Anonymous Monk on Oct 10, 2007 at 16:00 UTC
    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: $!";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://643983]
Approved by Mutant
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2024-04-25 11:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found