http://www.perlmonks.org?node_id=882990

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

How can I put a scalar into a string when there is no whitespace after the scalar so that perl knows where the scalar name ends? I could use concatenation or I could move the slash to the string but it seems like there would be another way.

my $tmp='./tmp/'; my @zips =("$tmpfile1.zip","$tmpfile2.zip");

Replies are listed 'Best First'.
Re: embedding a scalar into a string without space after the scalar
by jethro (Monsignor) on Jan 18, 2011 at 21:25 UTC
    "${tmp}file1.zip"
      You can find the description of this behaviour here in the documentation. Look for the paragraph beginning with "As in some shells".
Re: embedding a scalar into a string without space after the scalar
by toolic (Bishop) on Jan 18, 2011 at 21:46 UTC
    Here's another approach using map:
    use warnings; use strict; use Data::Dumper; my $tmp = './tmp/'; my @zips = map { "$tmp$_" } qw(file1.zip file2.zip); print Dumper(\@zips); __END__ $VAR1 = [ './tmp/file1.zip', './tmp/file2.zip' ];
Re: embedding a scalar into a string without space after the scalar
by eff_i_g (Curate) on Jan 18, 2011 at 22:27 UTC
    Use curlies as stated above. For this situation I prefer:
    use Path::Class; my $tmp='./tmp/'; my @zips = map { file($tmp, $_) } qw(file1.zip file2.zip);
Re: embedding a scalar into a string without space after the scalar
by thezip (Vicar) on Jan 19, 2011 at 00:57 UTC

    Gulliver,

    I'm not sure this directly answers your question, but it does show another way to accomplish what I think you are trying to do:

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $tmp ='./tmp/'; my $filename_template = '%sfile%02d.zip'; my @zips = map { sprintf $filename_template, $tmp, $_ } (1 .. 5); print Dumper(\@zips);
    __OUTPUT__ $VAR1 = [ './tmp/file01.zip', './tmp/file02.zip', './tmp/file03.zip', './tmp/file04.zip', './tmp/file05.zip' ];
    I don't think it's a good idea to store the scalar in the string. Much better to provide a means to generate the name programmatically, IMHO.

    What can be asserted without proof can be dismissed without proof. - Christopher Hitchens
Re: embedding a scalar into a string without space after the scalar
by TomDLux (Vicar) on Jan 19, 2011 at 00:47 UTC

    Using the curlies is an important trick to remember if you ever have to use shell scripts, cause bash or tcsh don't have map{}!

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Re: embedding a scalar into a string without space after the scalar
by llancet (Friar) on Jan 19, 2011 at 02:09 UTC
    It seems you actually needs:
    use File::Spec::Functions; # got './tmp/file1.zip' on unix # got '.\tmp\file1.zip' on windows my $path = catfile '.', 'tmp', 'file1.zip';
Re: embedding a scalar into a string without space after the scalar
by fullermd (Priest) on Jan 20, 2011 at 01:41 UTC

    As a side matter (and avoiding the issue, for bonus points) I've generally found that putting the trailing slashes on dir variables makes my life harder and my code uglier, so I'd skip that anyway.

    my $tmp='./tmp'; my @zips =("$tmp/file1.zip","$tmp/file2.zip");