Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Use scalars to define a filename

by akrrs7 (Acolyte)
on Oct 17, 2011 at 14:03 UTC ( [id://931928]=perlquestion: print w/replies, xml ) Need Help??

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

Hello, I am trying to use scalar variables to define a file name - something like this:
my $var1 = "thisItem1"; my $var2 = "01"; $ENV{'scriptDir'} = "c:/temp"; my $fileLocn = $ENV{'scriptDir'}; my $fileName = "fileLocn/$var1_$var2.txt";
The expected result for $fileName should be c:/temp/thisItem1_01.txt However, I am getting errors like - string found or bareword found where operator expected. What is the correct syntax for defining $fileName ? Thanks...

Replies are listed 'Best First'.
Re: Use scalars to define a filename
by toolic (Bishop) on Oct 17, 2011 at 14:11 UTC
    Perl thinks you have a variable named $var1_.
    my $fileName = "fileLocn/$var1_$var2.txt";
    Change that to:
    my $fileName = "fileLocn/${var1}_$var2.txt";
    Scalar value constructors
      Thanks Toolic...that fixed it.
Re: Use scalars to define a filename
by Ovid (Cardinal) on Oct 17, 2011 at 14:13 UTC

    There are a couple of issues here. First, when you get the "string found or bareword..." error message, it will include a line number. That line number is likely not the line you're pointing to because there are no barewords or syntax errors in the code above. You'll want to post a larger code snippet.

    That being said, in Perl an identifier should start with a letter or underscore and is followed by zero or more word characters. A scalar variable in Perl is defined as a dollar sign ($) followed by an identifier. When you have a this line:

    my $filename = "fileLocn/$var1_$var2.txt";

    When you fix your other issue, you'll probably get an error message similar to: Global symbol "$var1_" requires explicit package name at temp.pl line 10.

    (Note: If you're on a version of Perl less than 5.10, you won't see the variable name)

    Perl thinks that $var1_ is a variable name. You need to tell Perl that the trailing underscore (_) is not part of the variable name. You can fix this one of two ways:

    # escape the underscore my $fileName = "fileLocn/$var1\_$var2.txt"; # or wrap the identifier in curly braces my $fileName = "fileLocn/${var1}_$var2.txt";

    Also, I suspect that you wanted fileLocn to begin with a dollar sign?

Re: Use scalars to define a filename
by ikegami (Patriarch) on Oct 17, 2011 at 14:15 UTC

    The code you have does not result in that error. The code you have does result in a *different* error under use strict, though.

    "fileLocn/$var1_$var2.txt"
    is the same as
    "fileLocn/" . $var1_ . $var2 . ".txt"

    The problem being that there is no variable $var1_. Fix:

    "fileLocn/${var1}_$var2.txt"

    [ Whoa! three people posted more or less the same while I was typing this! ]

Re: Use scalars to define a filename
by Utilitarian (Vicar) on Oct 17, 2011 at 14:13 UTC
    The underscore is a legitimate character in a variable name,so you'll have to create to the file in one of the following ways
    my $fileName = "fileLocn/".$var1."_$var2.txt"; my $fileName = "fileLocn/${var1}_$var2.txt"; my $fileName = sprintf("fileLocn/%s_%s.txt", $var1, $var2);
    Or one a a myriad others

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: Use scalars to define a filename
by Anonymous Monk on Oct 17, 2011 at 14:06 UTC

Log In?
Username:
Password:

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

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

    No recent polls found